feat: make token_type_hint optional (#3397)

This commit is contained in:
DacongDA 2024-12-04 20:10:15 +08:00 committed by GitHub
parent 2ceb54f058
commit 6175fd6764
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 36 deletions

View File

@ -322,7 +322,9 @@ func (c *ApiController) IntrospectToken() {
} }
tokenTypeHint := c.Input().Get("token_type_hint") tokenTypeHint := c.Input().Get("token_type_hint")
token, err := object.GetTokenByTokenValue(tokenValue, tokenTypeHint) var token *object.Token
if tokenTypeHint != "" {
token, err = object.GetTokenByTokenValue(tokenValue, tokenTypeHint)
if err != nil { if err != nil {
c.ResponseTokenError(err.Error()) c.ResponseTokenError(err.Error())
return return
@ -332,6 +334,9 @@ func (c *ApiController) IntrospectToken() {
c.ServeJSON() c.ServeJSON()
return return
} }
}
var introspectionResponse object.IntrospectionResponse
if application.TokenFormat == "JWT-Standard" { if application.TokenFormat == "JWT-Standard" {
jwtToken, err := object.ParseStandardJwtTokenByApplication(tokenValue, application) jwtToken, err := object.ParseStandardJwtTokenByApplication(tokenValue, application)
@ -344,12 +349,12 @@ func (c *ApiController) IntrospectToken() {
return return
} }
c.Data["json"] = &object.IntrospectionResponse{ introspectionResponse = object.IntrospectionResponse{
Active: true, Active: true,
Scope: jwtToken.Scope, Scope: jwtToken.Scope,
ClientId: clientId, ClientId: clientId,
Username: token.User, Username: jwtToken.Name,
TokenType: token.TokenType, TokenType: jwtToken.TokenType,
Exp: jwtToken.ExpiresAt.Unix(), Exp: jwtToken.ExpiresAt.Unix(),
Iat: jwtToken.IssuedAt.Unix(), Iat: jwtToken.IssuedAt.Unix(),
Nbf: jwtToken.NotBefore.Unix(), Nbf: jwtToken.NotBefore.Unix(),
@ -358,10 +363,7 @@ func (c *ApiController) IntrospectToken() {
Iss: jwtToken.Issuer, Iss: jwtToken.Issuer,
Jti: jwtToken.ID, Jti: jwtToken.ID,
} }
c.ServeJSON() } else {
return
}
jwtToken, err := object.ParseJwtTokenByApplication(tokenValue, application) jwtToken, err := object.ParseJwtTokenByApplication(tokenValue, application)
if err != nil || jwtToken.Valid() != nil { if err != nil || jwtToken.Valid() != nil {
// and token revoked case. but we not implement // and token revoked case. but we not implement
@ -372,12 +374,12 @@ func (c *ApiController) IntrospectToken() {
return return
} }
c.Data["json"] = &object.IntrospectionResponse{ introspectionResponse = object.IntrospectionResponse{
Active: true, Active: true,
Scope: jwtToken.Scope, Scope: jwtToken.Scope,
ClientId: clientId, ClientId: clientId,
Username: token.User, Username: jwtToken.Name,
TokenType: token.TokenType, TokenType: jwtToken.TokenType,
Exp: jwtToken.ExpiresAt.Unix(), Exp: jwtToken.ExpiresAt.Unix(),
Iat: jwtToken.IssuedAt.Unix(), Iat: jwtToken.IssuedAt.Unix(),
Nbf: jwtToken.NotBefore.Unix(), Nbf: jwtToken.NotBefore.Unix(),
@ -386,5 +388,21 @@ func (c *ApiController) IntrospectToken() {
Iss: jwtToken.Issuer, Iss: jwtToken.Issuer,
Jti: jwtToken.ID, Jti: jwtToken.ID,
} }
}
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"] = introspectionResponse
c.ServeJSON() c.ServeJSON()
} }

View File

@ -124,6 +124,7 @@ func GetTokenByRefreshToken(refreshToken string) (*Token, error) {
func GetTokenByTokenValue(tokenValue, tokenTypeHint string) (*Token, error) { func GetTokenByTokenValue(tokenValue, tokenTypeHint string) (*Token, error) {
switch tokenTypeHint { switch tokenTypeHint {
case "access_token": case "access_token":
case "access-token":
token, err := GetTokenByAccessToken(tokenValue) token, err := GetTokenByAccessToken(tokenValue)
if err != nil { if err != nil {
return nil, err return nil, err
@ -132,6 +133,7 @@ func GetTokenByTokenValue(tokenValue, tokenTypeHint string) (*Token, error) {
return token, nil return token, nil
} }
case "refresh_token": case "refresh_token":
case "refresh-token":
token, err := GetTokenByRefreshToken(tokenValue) token, err := GetTokenByRefreshToken(tokenValue)
if err != nil { if err != nil {
return nil, err return nil, err