auth.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package auth
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "github.com/gravitl/netmaker/logger"
  6. "github.com/gravitl/netmaker/logic"
  7. "github.com/gravitl/netmaker/models"
  8. "golang.org/x/crypto/bcrypt"
  9. "golang.org/x/oauth2"
  10. )
  11. // == consts ==
  12. const (
  13. node_signin_length = 64
  14. )
  15. var (
  16. auth_provider *oauth2.Config
  17. )
  18. // IsOauthUser - returns
  19. func IsOauthUser(user *models.User) error {
  20. var currentValue, err = FetchPassValue("")
  21. if err != nil {
  22. return err
  23. }
  24. var bCryptErr = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(currentValue))
  25. return bCryptErr
  26. }
  27. func FetchPassValue(newValue string) (string, error) {
  28. type valueHolder struct {
  29. Value string `json:"value" bson:"value"`
  30. }
  31. newValueHolder := valueHolder{}
  32. var currentValue, err = logic.FetchAuthSecret()
  33. if err != nil {
  34. return "", err
  35. }
  36. var unmarshErr = json.Unmarshal([]byte(currentValue), &newValueHolder)
  37. if unmarshErr != nil {
  38. return "", unmarshErr
  39. }
  40. var b64CurrentValue, b64Err = base64.StdEncoding.DecodeString(newValueHolder.Value)
  41. if b64Err != nil {
  42. logger.Log(0, "could not decode pass")
  43. return "", nil
  44. }
  45. return string(b64CurrentValue), nil
  46. }
  47. func isUserIsAllowed(username, network string) (*models.User, error) {
  48. user, err := logic.GetUser(username)
  49. if err != nil { // user must not exist, so try to make one
  50. return &models.User{}, err
  51. }
  52. return user, nil
  53. }