Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 29, 2024
1 parent 199f8ab commit 7f6e790
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions handler/oauth2/flow_resource_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type ResourceOwnerPasswordCredentialsGrantHandler struct {
}
}

type Session interface {
// SetSubject sets the session's subject.
SetSubject(subject string)
}

// HandleTokenEndpointRequest implements https://tools.ietf.org/html/rfc6749#section-4.3.2
func (c *ResourceOwnerPasswordCredentialsGrantHandler) HandleTokenEndpointRequest(ctx context.Context, request fosite.AccessRequester) error {
if !c.CanHandleTokenEndpointRequest(ctx, request) {
Expand All @@ -58,13 +63,13 @@ func (c *ResourceOwnerPasswordCredentialsGrantHandler) HandleTokenEndpointReques
password := request.GetRequestForm().Get("password")
if username == "" || password == "" {
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint("Username or password are missing from the POST body."))
} else if identityID, err := c.ResourceOwnerPasswordCredentialsGrantStorage.Authenticate(ctx, username, password); errors.Is(err, fosite.ErrNotFound) {
} else if sub, err := c.ResourceOwnerPasswordCredentialsGrantStorage.Authenticate(ctx, username, password); errors.Is(err, fosite.ErrNotFound) {
return errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("Unable to authenticate the provided username and password credentials.").WithWrap(err).WithDebug(err.Error()))
} else if err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
} else {
if sess, ok := request.GetSession().(fosite.ExtraClaimsSession); ok {
sess.GetExtraClaims()["identity_id"] = identityID
if sess, ok := request.GetSession().(Session); ok {
sess.SetSubject(sub)
}
}

Expand Down
2 changes: 1 addition & 1 deletion handler/oauth2/flow_resource_owner_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type ResourceOwnerPasswordCredentialsGrantStorage interface {
Authenticate(ctx context.Context, name string, secret string) (string, error)
Authenticate(ctx context.Context, name string, secret string) (subject string, err error)
AccessTokenStorage
RefreshTokenStorage
}

0 comments on commit 7f6e790

Please sign in to comment.