From 202a94a8e5eaf68803ffb0eef4568ef50d95ce77 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sun, 14 Mar 2021 00:08:59 +0800 Subject: [PATCH] Add /api/oauth/token API. --- authz/authz.go | 1 + controllers/token.go | 11 +++++++++++ object/token.go | 26 ++++++++++++++++++++++++++ routers/router.go | 1 + web/src/TokenListPage.js | 12 ++++++------ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/authz/authz.go b/authz/authz.go index 1d4dfc78..914eb2fa 100644 --- a/authz/authz.go +++ b/authz/authz.go @@ -71,6 +71,7 @@ p, *, *, POST, /api/login, *, * p, *, *, POST, /api/logout, *, * p, *, *, GET, /api/get-account, *, * p, *, *, GET, /api/auth/login, *, * +p, *, *, GET, /api/oauth/token, *, * p, *, *, GET, /api/get-application, *, * p, *, *, GET, /api/get-users, *, * p, *, *, GET, /api/get-user, *, * diff --git a/controllers/token.go b/controllers/token.go index 0761bc9b..39135d29 100644 --- a/controllers/token.go +++ b/controllers/token.go @@ -68,3 +68,14 @@ func (c *ApiController) DeleteToken() { c.Data["json"] = object.DeleteToken(&token) 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() +} diff --git a/object/token.go b/object/token.go index 05b4bad6..19cc3757 100644 --- a/object/token.go +++ b/object/token.go @@ -93,3 +93,29 @@ func DeleteToken(token *Token) bool { 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 +} diff --git a/routers/router.go b/routers/router.go index d2d21c2f..b66352d4 100644 --- a/routers/router.go +++ b/routers/router.go @@ -69,4 +69,5 @@ func initAPI() { beego.Router("/api/update-token", &controllers.ApiController{}, "POST:UpdateToken") beego.Router("/api/add-token", &controllers.ApiController{}, "POST:AddToken") beego.Router("/api/delete-token", &controllers.ApiController{}, "POST:DeleteToken") + beego.Router("/api/oauth/token", &controllers.ApiController{}, "GET:GetOAuthToken") } diff --git a/web/src/TokenListPage.js b/web/src/TokenListPage.js index 9a3847a3..2d082b07 100644 --- a/web/src/TokenListPage.js +++ b/web/src/TokenListPage.js @@ -90,7 +90,7 @@ class TokenListPage extends React.Component { title: i18next.t("general:Name"), dataIndex: 'name', key: 'name', - width: '120px', + width: '300px', sorter: (a, b) => a.name.localeCompare(b.name), render: (text, record, index) => { return ( @@ -114,7 +114,7 @@ class TokenListPage extends React.Component { title: i18next.t("token:Application"), dataIndex: 'application', key: 'application', - width: '150px', + width: '120px', sorter: (a, b) => a.application.localeCompare(b.application), render: (text, record, index) => { return ( @@ -128,28 +128,28 @@ class TokenListPage extends React.Component { title: i18next.t("token:Access Token"), dataIndex: 'accessToken', key: 'accessToken', - width: '150px', + // width: '150px', sorter: (a, b) => a.accessToken.localeCompare(b.accessToken), }, { title: i18next.t("token:Expires In"), dataIndex: 'expiresIn', key: 'expiresIn', - width: '150px', + width: '120px', sorter: (a, b) => a.expiresIn - b.expiresIn, }, { title: i18next.t("token:Scope"), dataIndex: 'scope', key: 'scope', - width: '150px', + width: '100px', sorter: (a, b) => a.scope.localeCompare(b.scope), }, { title: i18next.t("token:Token Type"), dataIndex: 'tokenType', key: 'tokenType', - width: '150px', + width: '130px', sorter: (a, b) => a.tokenType.localeCompare(b.tokenType), }, {