feat: add Oauth 2.0 Token Introspection(rfc7662) endpoint support (#532)

Signed-off-by: Leon <leondevlifelog@gmail.com>
This commit is contained in:
Leon
2022-03-03 17:48:47 +08:00
committed by GitHub
parent e31aaf5657
commit ab5af979c8
4 changed files with 99 additions and 0 deletions

View File

@ -60,6 +60,29 @@ type TokenWrapper struct {
Scope string `json:"scope"`
}
type TokenIntrospectionRequest struct {
// access_token's value or refresh_token's value
Token string `json:"token"`
// pass this parameter to help the authorization server optimize the token lookup.
// value is one of `access_token` or `refresh_token`
TokenTypeHint string `json:"token_type_hint,omitempty"`
}
type IntrospectionResponse struct {
Active bool `json:"active"`
Scope string `json:"scope,omitempty"`
ClientId string `json:"client_id,omitempty"`
Username string `json:"username,omitempty"`
TokenType string `json:"token_type,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iat int64 `json:"iat,omitempty"`
Nbf int64 `json:"nbf,omitempty"`
Sub string `json:"sub,omitempty"`
Aud []string `json:"aud,omitempty"`
Iss string `json:"iss,omitempty"`
Jti string `json:"jti,omitempty"`
}
func GetTokenCount(owner, field, value string) int {
session := GetSession(owner, -1, -1, field, value, "", "")
count, err := session.Count(&Token{})
@ -198,6 +221,15 @@ func GetTokenByAccessToken(accessToken string) *Token {
return &token
}
func GetTokenByTokenAndApplication(token string, application string) *Token {
tokenResult := Token{}
existed, err := adapter.Engine.Where("(refresh_token = ? or access_token = ? ) and application = ?", token, token, application).Get(&tokenResult)
if err != nil || !existed {
return nil
}
return &tokenResult
}
func CheckOAuthLogin(clientId string, responseType string, redirectUri string, scope string, state string) (string, *Application) {
if responseType != "code" && responseType != "token" && responseType != "id_token" {
return fmt.Sprintf("error: grant_type: %s is not supported in this application", responseType), nil