|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/glasskube/distr/internal/authjwt" |
| 12 | + "github.com/glasskube/distr/internal/authn" |
| 13 | + "github.com/glasskube/distr/internal/authn/authinfo" |
| 14 | + "github.com/glasskube/distr/internal/authn/basic" |
| 15 | + "github.com/glasskube/distr/internal/db" |
| 16 | + "github.com/glasskube/distr/internal/security" |
| 17 | + "github.com/glasskube/distr/internal/types" |
| 18 | + "github.com/google/uuid" |
| 19 | +) |
| 20 | + |
| 21 | +func Authenticator() authn.Authenticator[basic.Auth, authinfo.AuthInfo] { |
| 22 | + fn := func(ctx context.Context, basic basic.Auth) (out authinfo.AuthInfo, err error) { |
| 23 | + if targetID, err1 := uuid.Parse(basic.Username); err1 != nil { |
| 24 | + err = fmt.Errorf("%w: %w", authn.ErrBadAuthentication, err1) |
| 25 | + } else if target, err1 := db.GetDeploymentTarget(ctx, targetID, nil); err1 != nil { |
| 26 | + err = err1 |
| 27 | + } else if target.AccessKeyHash == nil || target.AccessKeySalt == nil { |
| 28 | + err = errors.New("access key or salt is nil") |
| 29 | + } else if err1 := |
| 30 | + security.VerifyAccessKey(*target.AccessKeySalt, *target.AccessKeyHash, basic.Password); err1 != nil { |
| 31 | + err = fmt.Errorf("%w: %w", authn.ErrBadAuthentication, err1) |
| 32 | + } else if user, err1 := db.GetUserAccountByID(ctx, target.CreatedByUserAccountID); err1 != nil { |
| 33 | + err = err1 |
| 34 | + } else if orgs, err1 := db.GetOrganizationsForUser(ctx, user.ID); err1 != nil { |
| 35 | + err = err1 |
| 36 | + } else if len(orgs) != 1 { |
| 37 | + err = fmt.Errorf("user must have exactly one organization") |
| 38 | + } else if orgs[0].UserRole != types.UserRoleCustomer { |
| 39 | + err = fmt.Errorf("user must have role customer") |
| 40 | + } else if _, token, err1 := authjwt.GenerateDefaultToken(*user, orgs[0]); err1 != nil { |
| 41 | + err = err1 |
| 42 | + } else { |
| 43 | + err = &tokenError{Token: token} |
| 44 | + } |
| 45 | + return |
| 46 | + } |
| 47 | + return authn.AuthenticatorFunc[basic.Auth, authinfo.AuthInfo](fn) |
| 48 | +} |
| 49 | + |
| 50 | +type tokenError struct { |
| 51 | + Token string `json:"token"` |
| 52 | +} |
| 53 | + |
| 54 | +var _ authn.WithResponseHeaders = &tokenError{} |
| 55 | +var _ authn.WithResponseStatus = &tokenError{} |
| 56 | +var _ authn.ResponseBodyWriter = &tokenError{} |
| 57 | + |
| 58 | +// Error implements error. |
| 59 | +func (t *tokenError) Error() string { |
| 60 | + return "NOT AN ERROR: agent successful token auth" // :^) |
| 61 | +} |
| 62 | + |
| 63 | +// ResponseHeaders implements authn.WithResponseHeaders. |
| 64 | +func (t *tokenError) ResponseHeaders() http.Header { |
| 65 | + result := http.Header{} |
| 66 | + result.Add("Content-Type", "application/json") |
| 67 | + return result |
| 68 | +} |
| 69 | + |
| 70 | +// ResponseStatus implements authn.WithResponseStatus. |
| 71 | +func (t *tokenError) ResponseStatus() int { |
| 72 | + return http.StatusOK |
| 73 | +} |
| 74 | + |
| 75 | +// WriteResponse implements authn.ResponseBodyWriter. |
| 76 | +func (t *tokenError) WriteResponse(w io.Writer) { |
| 77 | + _ = json.NewEncoder(w).Encode(t) |
| 78 | +} |
0 commit comments