Add /api/oauth/token API.

This commit is contained in:
Yang Luo
2021-03-14 00:08:59 +08:00
parent 85523fa9d4
commit 202a94a8e5
5 changed files with 45 additions and 6 deletions

View File

@ -71,6 +71,7 @@ p, *, *, POST, /api/login, *, *
p, *, *, POST, /api/logout, *, * p, *, *, POST, /api/logout, *, *
p, *, *, GET, /api/get-account, *, * p, *, *, GET, /api/get-account, *, *
p, *, *, GET, /api/auth/login, *, * p, *, *, GET, /api/auth/login, *, *
p, *, *, GET, /api/oauth/token, *, *
p, *, *, GET, /api/get-application, *, * p, *, *, GET, /api/get-application, *, *
p, *, *, GET, /api/get-users, *, * p, *, *, GET, /api/get-users, *, *
p, *, *, GET, /api/get-user, *, * p, *, *, GET, /api/get-user, *, *

View File

@ -68,3 +68,14 @@ func (c *ApiController) DeleteToken() {
c.Data["json"] = object.DeleteToken(&token) c.Data["json"] = object.DeleteToken(&token)
c.ServeJSON() c.ServeJSON()
} }
func (c *ApiController) GetOAuthToken() {
applicationId := c.Input().Get("app_id")
grantType := c.Input().Get("grant_type")
clientId := c.Input().Get("client_id")
clientSecret := c.Input().Get("client_secret")
scope := c.Input().Get("scope")
c.Data["json"] = object.GetOAuthToken(applicationId, grantType, clientId, clientSecret, scope)
c.ServeJSON()
}

View File

@ -93,3 +93,29 @@ func DeleteToken(token *Token) bool {
return affected != 0 return affected != 0
} }
func GetOAuthToken(applicationId string, grantType string, clientId string, clientSecret string, scope string) *Token {
application := GetApplication(applicationId)
if grantType != "client_credentials" {
return nil
}
if application.ClientId != clientId || application.ClientSecret != clientSecret {
return nil
}
token := &Token{
Owner: application.Owner,
Name: util.GenerateId(),
CreatedTime: util.GetCurrentTime(),
Application: application.Name,
AccessToken: "",
ExpiresIn: 7200,
Scope: scope,
TokenType: "Bearer",
}
AddToken(token)
return token
}

View File

@ -69,4 +69,5 @@ func initAPI() {
beego.Router("/api/update-token", &controllers.ApiController{}, "POST:UpdateToken") beego.Router("/api/update-token", &controllers.ApiController{}, "POST:UpdateToken")
beego.Router("/api/add-token", &controllers.ApiController{}, "POST:AddToken") beego.Router("/api/add-token", &controllers.ApiController{}, "POST:AddToken")
beego.Router("/api/delete-token", &controllers.ApiController{}, "POST:DeleteToken") beego.Router("/api/delete-token", &controllers.ApiController{}, "POST:DeleteToken")
beego.Router("/api/oauth/token", &controllers.ApiController{}, "GET:GetOAuthToken")
} }

View File

@ -90,7 +90,7 @@ class TokenListPage extends React.Component {
title: i18next.t("general:Name"), title: i18next.t("general:Name"),
dataIndex: 'name', dataIndex: 'name',
key: 'name', key: 'name',
width: '120px', width: '300px',
sorter: (a, b) => a.name.localeCompare(b.name), sorter: (a, b) => a.name.localeCompare(b.name),
render: (text, record, index) => { render: (text, record, index) => {
return ( return (
@ -114,7 +114,7 @@ class TokenListPage extends React.Component {
title: i18next.t("token:Application"), title: i18next.t("token:Application"),
dataIndex: 'application', dataIndex: 'application',
key: 'application', key: 'application',
width: '150px', width: '120px',
sorter: (a, b) => a.application.localeCompare(b.application), sorter: (a, b) => a.application.localeCompare(b.application),
render: (text, record, index) => { render: (text, record, index) => {
return ( return (
@ -128,28 +128,28 @@ class TokenListPage extends React.Component {
title: i18next.t("token:Access Token"), title: i18next.t("token:Access Token"),
dataIndex: 'accessToken', dataIndex: 'accessToken',
key: 'accessToken', key: 'accessToken',
width: '150px', // width: '150px',
sorter: (a, b) => a.accessToken.localeCompare(b.accessToken), sorter: (a, b) => a.accessToken.localeCompare(b.accessToken),
}, },
{ {
title: i18next.t("token:Expires In"), title: i18next.t("token:Expires In"),
dataIndex: 'expiresIn', dataIndex: 'expiresIn',
key: 'expiresIn', key: 'expiresIn',
width: '150px', width: '120px',
sorter: (a, b) => a.expiresIn - b.expiresIn, sorter: (a, b) => a.expiresIn - b.expiresIn,
}, },
{ {
title: i18next.t("token:Scope"), title: i18next.t("token:Scope"),
dataIndex: 'scope', dataIndex: 'scope',
key: 'scope', key: 'scope',
width: '150px', width: '100px',
sorter: (a, b) => a.scope.localeCompare(b.scope), sorter: (a, b) => a.scope.localeCompare(b.scope),
}, },
{ {
title: i18next.t("token:Token Type"), title: i18next.t("token:Token Type"),
dataIndex: 'tokenType', dataIndex: 'tokenType',
key: 'tokenType', key: 'tokenType',
width: '150px', width: '130px',
sorter: (a, b) => a.tokenType.localeCompare(b.tokenType), sorter: (a, b) => a.tokenType.localeCompare(b.tokenType),
}, },
{ {