mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
feat: The /login/oauth/access_token api supports the token and id_token grant types. (#2836)
* In the response of the /api/get-captcha endpoint, add the parameters "owner" and "name" because these two parameters will be used when calling the /api/verify-captcha endpoint. * The /login/oauth/access_token api supports the token and id_token grant types.
This commit is contained in:
parent
b158b840bd
commit
d054f3e001
@ -164,6 +164,7 @@ func (c *ApiController) GetOAuthToken() {
|
|||||||
code := c.Input().Get("code")
|
code := c.Input().Get("code")
|
||||||
verifier := c.Input().Get("code_verifier")
|
verifier := c.Input().Get("code_verifier")
|
||||||
scope := c.Input().Get("scope")
|
scope := c.Input().Get("scope")
|
||||||
|
nonce := c.Input().Get("nonce")
|
||||||
username := c.Input().Get("username")
|
username := c.Input().Get("username")
|
||||||
password := c.Input().Get("password")
|
password := c.Input().Get("password")
|
||||||
tag := c.Input().Get("tag")
|
tag := c.Input().Get("tag")
|
||||||
@ -197,6 +198,9 @@ func (c *ApiController) GetOAuthToken() {
|
|||||||
if scope == "" {
|
if scope == "" {
|
||||||
scope = tokenRequest.Scope
|
scope = tokenRequest.Scope
|
||||||
}
|
}
|
||||||
|
if nonce == "" {
|
||||||
|
nonce = tokenRequest.Nonce
|
||||||
|
}
|
||||||
if username == "" {
|
if username == "" {
|
||||||
username = tokenRequest.Username
|
username = tokenRequest.Username
|
||||||
}
|
}
|
||||||
@ -216,7 +220,7 @@ func (c *ApiController) GetOAuthToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
host := c.Ctx.Request.Host
|
host := c.Ctx.Request.Host
|
||||||
token, err := object.GetOAuthToken(grantType, clientId, clientSecret, code, verifier, scope, username, password, host, refreshToken, tag, avatar, c.GetAcceptLanguage())
|
token, err := object.GetOAuthToken(grantType, clientId, clientSecret, code, verifier, scope, nonce, username, password, host, refreshToken, tag, avatar, c.GetAcceptLanguage())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
|
@ -21,6 +21,7 @@ type TokenRequest struct {
|
|||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Verifier string `json:"code_verifier"`
|
Verifier string `json:"code_verifier"`
|
||||||
Scope string `json:"scope"`
|
Scope string `json:"scope"`
|
||||||
|
Nonce string `json:"nonce"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Tag string `json:"tag"`
|
Tag string `json:"tag"`
|
||||||
|
@ -189,7 +189,7 @@ func GetOAuthCode(userId string, clientId string, responseType string, redirectU
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOAuthToken(grantType string, clientId string, clientSecret string, code string, verifier string, scope string, username string, password string, host string, refreshToken string, tag string, avatar string, lang string) (interface{}, error) {
|
func GetOAuthToken(grantType string, clientId string, clientSecret string, code string, verifier string, scope string, nonce string, username string, password string, host string, refreshToken string, tag string, avatar string, lang string) (interface{}, error) {
|
||||||
application, err := GetApplicationByClientId(clientId)
|
application, err := GetApplicationByClientId(clientId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -220,6 +220,8 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code
|
|||||||
token, tokenError, err = GetPasswordToken(application, username, password, scope, host)
|
token, tokenError, err = GetPasswordToken(application, username, password, scope, host)
|
||||||
case "client_credentials": // Client Credentials Grant
|
case "client_credentials": // Client Credentials Grant
|
||||||
token, tokenError, err = GetClientCredentialsToken(application, clientSecret, scope, host)
|
token, tokenError, err = GetClientCredentialsToken(application, clientSecret, scope, host)
|
||||||
|
case "token", "id_token": // Implicit Grant
|
||||||
|
token, tokenError, err = GetImplicitToken(application, username, scope, nonce, host)
|
||||||
case "refresh_token":
|
case "refresh_token":
|
||||||
refreshToken2, err := RefreshToken(grantType, refreshToken, scope, clientId, clientSecret, host)
|
refreshToken2, err := RefreshToken(grantType, refreshToken, scope, clientId, clientSecret, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -582,6 +584,33 @@ func GetClientCredentialsToken(application *Application, clientSecret string, sc
|
|||||||
return token, nil, nil
|
return token, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetImplicitToken
|
||||||
|
// Implicit flow
|
||||||
|
func GetImplicitToken(application *Application, username string, scope string, nonce string, host string) (*Token, *TokenError, error) {
|
||||||
|
user, err := GetUserByFields(application.Organization, username)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return nil, &TokenError{
|
||||||
|
Error: InvalidGrant,
|
||||||
|
ErrorDescription: "the user does not exist",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
if user.IsForbidden {
|
||||||
|
return nil, &TokenError{
|
||||||
|
Error: InvalidGrant,
|
||||||
|
ErrorDescription: "the user is forbidden to sign in, please contact the administrator",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := GetTokenByUser(application, user, scope, nonce, host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return token, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetTokenByUser
|
// GetTokenByUser
|
||||||
// Implicit flow
|
// Implicit flow
|
||||||
func GetTokenByUser(application *Application, user *User, scope string, nonce string, host string) (*Token, error) {
|
func GetTokenByUser(application *Application, user *User, scope string, nonce string, host string) (*Token, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user