From 58c7a60220856c0bdb06fbadbd20213351c0c0d9 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Tue, 1 Jun 2021 21:14:41 +0800 Subject: [PATCH] Show error in AccessToken. --- object/token.go | 19 ++++++++++++++----- object/token_jwt.go | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/object/token.go b/object/token.go index 37b2e6b6..78ccd6ee 100644 --- a/object/token.go +++ b/object/token.go @@ -195,7 +195,7 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code application := getApplicationByClientId(clientId) if application == nil { return &TokenWrapper{ - AccessToken: "Invalid client_id", + AccessToken: "error: invalid client_id", TokenType: "", ExpiresIn: 0, Scope: "", @@ -204,7 +204,16 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code if grantType != "authorization_code" { return &TokenWrapper{ - AccessToken: "grant_type should be \"authorization_code\"", + AccessToken: "error: grant_type should be \"authorization_code\"", + TokenType: "", + ExpiresIn: 0, + Scope: "", + } + } + + if code == "" { + return &TokenWrapper{ + AccessToken: "error: code should not be empty", TokenType: "", ExpiresIn: 0, Scope: "", @@ -214,7 +223,7 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code token := getTokenByCode(code) if token == nil { return &TokenWrapper{ - AccessToken: "Invalid code", + AccessToken: "error: invalid code", TokenType: "", ExpiresIn: 0, Scope: "", @@ -223,7 +232,7 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code if application.Name != token.Application { return &TokenWrapper{ - AccessToken: "The token is for wrong application (client_id)", + AccessToken: "error: the token is for wrong application (client_id)", TokenType: "", ExpiresIn: 0, Scope: "", @@ -232,7 +241,7 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code if application.ClientSecret != clientSecret { return &TokenWrapper{ - AccessToken: "Invalid client_secret", + AccessToken: "error: invalid client_secret", TokenType: "", ExpiresIn: 0, Scope: "", diff --git a/object/token_jwt.go b/object/token_jwt.go index a89a197f..bcf8c598 100644 --- a/object/token_jwt.go +++ b/object/token_jwt.go @@ -32,6 +32,8 @@ type Claims struct { Phone string `json:"phone"` Affiliation string `json:"affiliation"` Tag string `json:"tag"` + Language string `json:"language"` + Score int `json:"score"` IsAdmin bool `json:"isAdmin"` jwt.StandardClaims } @@ -50,6 +52,8 @@ func generateJwtToken(application *Application, user *User) (string, error) { Phone: user.Phone, Affiliation: user.Affiliation, Tag: user.Tag, + Language: user.Language, + Score: user.Score, IsAdmin: user.IsAdmin, StandardClaims: jwt.StandardClaims{ Audience: application.ClientId,