Skip to content

Commit

Permalink
feat: bearer access token includes tokenid and subject (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
hifabienne authored Oct 15, 2020
1 parent 4932464 commit 9943f20
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/internal/mock/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *AuthStorage) AuthorizeClientIDSecret(_ context.Context, id string, _ st
return nil
}

func (s *AuthStorage) GetUserinfoFromToken(ctx context.Context, _, _ string) (*oidc.Userinfo, error) {
func (s *AuthStorage) GetUserinfoFromToken(ctx context.Context, _, _, _ string) (*oidc.Userinfo, error) {
return s.GetUserinfoFromScopes(ctx, "", []string{})
}
func (s *AuthStorage) GetUserinfoFromScopes(_ context.Context, _ string, _ []string) (*oidc.Userinfo, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/op/mock/storage.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type OPStorage interface {
GetClientByClientID(context.Context, string) (Client, error)
AuthorizeClientIDSecret(context.Context, string, string) error
GetUserinfoFromScopes(context.Context, string, []string) (*oidc.Userinfo, error)
GetUserinfoFromToken(context.Context, string, string) (*oidc.Userinfo, error)
GetUserinfoFromToken(ctx context.Context, tokenID, subject, origin string) (*oidc.Userinfo, error)
GetKeyByIDAndUserID(ctx context.Context, keyID, userID string) (*jose.JSONWebKey, error)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/op/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func CreateAccessToken(ctx context.Context, authReq TokenRequest, accessTokenTyp
token, err = CreateJWT(creator.Issuer(), authReq, exp, id, creator.Signer())
return
}
token, err = CreateBearerToken(id, creator.Crypto())
token, err = CreateBearerToken(id, authReq.GetSubject(), creator.Crypto())
return
}

func CreateBearerToken(id string, crypto Crypto) (string, error) {
return crypto.Encrypt(id)
func CreateBearerToken(tokenID, subject string, crypto Crypto) (string, error) {
return crypto.Encrypt(tokenID + ":" + subject)
}

func CreateJWT(issuer string, authReq TokenRequest, exp time.Time, id string, signer Signer) (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/op/userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func Userinfo(w http.ResponseWriter, r *http.Request, userinfoProvider UserinfoP
http.Error(w, "access token missing", http.StatusUnauthorized)
return
}
tokenID, err := userinfoProvider.Crypto().Decrypt(accessToken)
tokenIDSubject, err := userinfoProvider.Crypto().Decrypt(accessToken)
if err != nil {
http.Error(w, "access token missing", http.StatusUnauthorized)
return
}
info, err := userinfoProvider.Storage().GetUserinfoFromToken(r.Context(), tokenID, r.Header.Get("origin"))
splittedToken := strings.Split(tokenIDSubject, ":")
info, err := userinfoProvider.Storage().GetUserinfoFromToken(r.Context(), splittedToken[0], splittedToken[1], r.Header.Get("origin"))
if err != nil {
w.WriteHeader(http.StatusForbidden)
utils.MarshalJSON(w, err)
Expand Down

0 comments on commit 9943f20

Please sign in to comment.