mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve code format.
This commit is contained in:
parent
d129202b95
commit
479daf4fa4
@ -106,8 +106,8 @@ func getToken(owner string, name string) *Token {
|
||||
}
|
||||
|
||||
func getTokenByCode(code string) *Token {
|
||||
token := Token{}
|
||||
existed, err := adapter.Engine.Where("code=?", code).Get(&token)
|
||||
token := Token{Code: code}
|
||||
existed, err := adapter.Engine.Get(&token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -158,8 +158,8 @@ func DeleteToken(token *Token) bool {
|
||||
|
||||
func GetTokenByAccessToken(accessToken string) *Token {
|
||||
//Check if the accessToken is in the database
|
||||
token := Token{}
|
||||
existed, err := adapter.Engine.Where("access_token=?", accessToken).Get(&token)
|
||||
token := Token{AccessToken: accessToken}
|
||||
existed, err := adapter.Engine.Get(&token)
|
||||
if err != nil || !existed {
|
||||
return nil
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ func AutoSigninFilter(ctx *context.Context) {
|
||||
// return
|
||||
//}
|
||||
|
||||
// "/page?access_token=123" or HTTP Bearer token
|
||||
// Authorization: Bearer bearerToken
|
||||
// GET parameter like "/page?access_token=123" or
|
||||
// HTTP Bearer token like "Authorization: Bearer 123"
|
||||
accessToken := ctx.Input.Query("accessToken")
|
||||
if accessToken == "" {
|
||||
accessToken = parseBearerToken(ctx)
|
||||
@ -36,13 +36,15 @@ func AutoSigninFilter(ctx *context.Context) {
|
||||
if accessToken != "" {
|
||||
token := object.GetTokenByAccessToken(accessToken)
|
||||
if token == nil {
|
||||
responseError(ctx, "non-existent accessToken")
|
||||
responseError(ctx, "Access token doesn't exist")
|
||||
return
|
||||
}
|
||||
if !util.CheckTokenExpireTime(token.CreatedTime, token.ExpiresIn) {
|
||||
responseError(ctx, "expired accessToken")
|
||||
|
||||
if !util.IsTokenExpired(token.CreatedTime, token.ExpiresIn) {
|
||||
responseError(ctx, "Access token has expired")
|
||||
return
|
||||
}
|
||||
|
||||
userId := fmt.Sprintf("%s/%s", token.Organization, token.User)
|
||||
application, _ := object.GetApplicationByUserId(fmt.Sprintf("app/%s", token.Application))
|
||||
setSessionUser(ctx, userId)
|
||||
|
@ -29,9 +29,8 @@ func GetCurrentUnixTime() string {
|
||||
return strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
}
|
||||
|
||||
func CheckTokenExpireTime(createdTime string, expireIn int) bool {
|
||||
create, _ := time.Parse(time.RFC3339, createdTime)
|
||||
expireAt := create.Add(time.Duration(expireIn) * time.Minute)
|
||||
|
||||
return time.Now().Before(expireAt)
|
||||
func IsTokenExpired(createdTime string, expiresIn int) bool {
|
||||
createdTimeObj, _ := time.Parse(time.RFC3339, createdTime)
|
||||
expiresAtObj := createdTimeObj.Add(time.Duration(expiresIn) * time.Minute)
|
||||
return time.Now().Before(expiresAtObj)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user