From 6175fd6764a2781d0f39d1b99935a404780a994b Mon Sep 17 00:00:00 2001 From: DacongDA Date: Wed, 4 Dec 2024 20:10:15 +0800 Subject: [PATCH] feat: make token_type_hint optional (#3397) --- controllers/token.go | 90 ++++++++++++++++++++++++++------------------ object/token.go | 2 + 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/controllers/token.go b/controllers/token.go index 13a3e9ec..9900a060 100644 --- a/controllers/token.go +++ b/controllers/token.go @@ -322,17 +322,22 @@ func (c *ApiController) IntrospectToken() { } tokenTypeHint := c.Input().Get("token_type_hint") - token, err := object.GetTokenByTokenValue(tokenValue, tokenTypeHint) - if err != nil { - c.ResponseTokenError(err.Error()) - return - } - if token == nil { - c.Data["json"] = &object.IntrospectionResponse{Active: false} - c.ServeJSON() - return + var token *object.Token + if tokenTypeHint != "" { + token, err = object.GetTokenByTokenValue(tokenValue, tokenTypeHint) + if err != nil { + c.ResponseTokenError(err.Error()) + return + } + if token == nil { + c.Data["json"] = &object.IntrospectionResponse{Active: false} + c.ServeJSON() + return + } } + var introspectionResponse object.IntrospectionResponse + if application.TokenFormat == "JWT-Standard" { jwtToken, err := object.ParseStandardJwtTokenByApplication(tokenValue, application) if err != nil || jwtToken.Valid() != nil { @@ -344,12 +349,37 @@ func (c *ApiController) IntrospectToken() { return } - c.Data["json"] = &object.IntrospectionResponse{ + introspectionResponse = object.IntrospectionResponse{ Active: true, Scope: jwtToken.Scope, ClientId: clientId, - Username: token.User, - TokenType: token.TokenType, + Username: jwtToken.Name, + TokenType: jwtToken.TokenType, + Exp: jwtToken.ExpiresAt.Unix(), + Iat: jwtToken.IssuedAt.Unix(), + Nbf: jwtToken.NotBefore.Unix(), + Sub: jwtToken.Subject, + Aud: jwtToken.Audience, + Iss: jwtToken.Issuer, + Jti: jwtToken.ID, + } + } else { + jwtToken, err := object.ParseJwtTokenByApplication(tokenValue, application) + if err != nil || jwtToken.Valid() != nil { + // and token revoked case. but we not implement + // TODO: 2022-03-03 add token revoked check, when we implemented the Token Revocation(rfc7009) Specs. + // refs: https://tools.ietf.org/html/rfc7009 + c.Data["json"] = &object.IntrospectionResponse{Active: false} + c.ServeJSON() + return + } + + introspectionResponse = object.IntrospectionResponse{ + Active: true, + Scope: jwtToken.Scope, + ClientId: clientId, + Username: jwtToken.Name, + TokenType: jwtToken.TokenType, Exp: jwtToken.ExpiresAt.Unix(), Iat: jwtToken.IssuedAt.Unix(), Nbf: jwtToken.NotBefore.Unix(), @@ -358,33 +388,21 @@ func (c *ApiController) IntrospectToken() { Iss: jwtToken.Issuer, Jti: jwtToken.ID, } - c.ServeJSON() - return } - jwtToken, err := object.ParseJwtTokenByApplication(tokenValue, application) - if err != nil || jwtToken.Valid() != nil { - // and token revoked case. but we not implement - // TODO: 2022-03-03 add token revoked check, when we implemented the Token Revocation(rfc7009) Specs. - // refs: https://tools.ietf.org/html/rfc7009 - c.Data["json"] = &object.IntrospectionResponse{Active: false} - c.ServeJSON() - return + if tokenTypeHint == "" { + token, err = object.GetTokenByTokenValue(tokenValue, introspectionResponse.TokenType) + if err != nil { + c.ResponseTokenError(err.Error()) + return + } + if token == nil { + c.Data["json"] = &object.IntrospectionResponse{Active: false} + c.ServeJSON() + return + } } - c.Data["json"] = &object.IntrospectionResponse{ - Active: true, - Scope: jwtToken.Scope, - ClientId: clientId, - Username: token.User, - TokenType: token.TokenType, - Exp: jwtToken.ExpiresAt.Unix(), - Iat: jwtToken.IssuedAt.Unix(), - Nbf: jwtToken.NotBefore.Unix(), - Sub: jwtToken.Subject, - Aud: jwtToken.Audience, - Iss: jwtToken.Issuer, - Jti: jwtToken.ID, - } + c.Data["json"] = introspectionResponse c.ServeJSON() } diff --git a/object/token.go b/object/token.go index 3125e5b4..fd1db5cf 100644 --- a/object/token.go +++ b/object/token.go @@ -124,6 +124,7 @@ func GetTokenByRefreshToken(refreshToken string) (*Token, error) { func GetTokenByTokenValue(tokenValue, tokenTypeHint string) (*Token, error) { switch tokenTypeHint { case "access_token": + case "access-token": token, err := GetTokenByAccessToken(tokenValue) if err != nil { return nil, err @@ -132,6 +133,7 @@ func GetTokenByTokenValue(tokenValue, tokenTypeHint string) (*Token, error) { return token, nil } case "refresh_token": + case "refresh-token": token, err := GetTokenByRefreshToken(tokenValue) if err != nil { return nil, err