Skip to content

Commit

Permalink
feat: add OIDC feature support. (#373)
Browse files Browse the repository at this point in the history
1. add nonce parameter.
2. add sub in userinfo endpoint.

Signed-off-by: 0x2a <stevesough@gmail.com>
  • Loading branch information
Steve0x2a committed Dec 15, 2021
1 parent 370e835 commit 98f6cc0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
11 changes: 9 additions & 2 deletions controllers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type RequestForm struct {
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
Sub string `json:"sub"`
Data interface{} `json:"data"`
Data2 interface{} `json:"data2"`
}
Expand Down Expand Up @@ -217,8 +218,14 @@ func (c *ApiController) GetAccount() {
}

organization := object.GetMaskedOrganization(object.GetOrganizationByUser(user))

c.ResponseOk(user, organization)
resp := Response{
Status: "ok",
Sub: userId,
Data: user,
Data2: organization,
}
c.Data["json"] = resp
c.ServeJSON()
}

// GetHumanCheck ...
Expand Down
4 changes: 2 additions & 2 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
redirectUri := c.Input().Get("redirectUri")
scope := c.Input().Get("scope")
state := c.Input().Get("state")

code := object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state)
nonce := c.Input().Get("nonce")
code := object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state, nonce)
resp = codeToResponse(code)

if application.HasPromptPage() {
Expand Down
3 changes: 2 additions & 1 deletion controllers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ func (c *ApiController) GetOAuthCode() {
redirectUri := c.Input().Get("redirect_uri")
scope := c.Input().Get("scope")
state := c.Input().Get("state")
nonce := c.Input().Get("nonce")

c.Data["json"] = object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state)
c.Data["json"] = object.GetOAuthCode(userId, clientId, responseType, redirectUri, scope, state, nonce)
c.ServeJSON()
}

Expand Down
4 changes: 2 additions & 2 deletions object/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func CheckOAuthLogin(clientId string, responseType string, redirectUri string, s
return "", application
}

func GetOAuthCode(userId string, clientId string, responseType string, redirectUri string, scope string, state string) *Code {
func GetOAuthCode(userId string, clientId string, responseType string, redirectUri string, scope string, state string, nonce string) *Code {
user := GetUser(userId)
if user == nil {
return &Code{
Expand All @@ -192,7 +192,7 @@ func GetOAuthCode(userId string, clientId string, responseType string, redirectU
}
}

accessToken, err := generateJwtToken(application, user)
accessToken, err := generateJwtToken(application, user, nonce)
if err != nil {
panic(err)
}
Expand Down
6 changes: 4 additions & 2 deletions object/token_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ var tokenJwtPrivateKey string

type Claims struct {
User
Nonce string `json:"nonce,omitempty"`
jwt.RegisteredClaims
}

func generateJwtToken(application *Application, user *User) (string, error) {
func generateJwtToken(application *Application, user *User, nonce string) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(time.Duration(application.ExpireInHours) * time.Hour)

user.Password = ""

claims := Claims{
User: *user,
User: *user,
Nonce: nonce,
RegisteredClaims: jwt.RegisteredClaims{
Issuer: beego.AppConfig.String("origin"),
Subject: user.Id,
Expand Down
2 changes: 1 addition & 1 deletion web/src/auth/AuthBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function oAuthParamsToQuery(oAuthParams) {
}

// code
return `?clientId=${oAuthParams.clientId}&responseType=${oAuthParams.responseType}&redirectUri=${oAuthParams.redirectUri}&scope=${oAuthParams.scope}&state=${oAuthParams.state}`;
return `?clientId=${oAuthParams.clientId}&responseType=${oAuthParams.responseType}&redirectUri=${oAuthParams.redirectUri}&scope=${oAuthParams.scope}&state=${oAuthParams.state}&nonce=${oAuthParams.nonce}`;
}

export function getApplicationLogin(oAuthParams) {
Expand Down
2 changes: 2 additions & 0 deletions web/src/auth/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function getOAuthGetParameters(params) {
const redirectUri = queries.get("redirect_uri");
const scope = queries.get("scope");
const state = queries.get("state");
const nonce = queries.get("nonce")

if (clientId === undefined || clientId === null) {
// login
Expand All @@ -94,6 +95,7 @@ export function getOAuthGetParameters(params) {
redirectUri: redirectUri,
scope: scope,
state: state,
nonce: nonce,
};
}
}
Expand Down

0 comments on commit 98f6cc0

Please sign in to comment.