Skip to content

Commit

Permalink
feat: add protection against attacks (casdoor#460)
Browse files Browse the repository at this point in the history
Signed-off-by: 0x2a <[email protected]>
  • Loading branch information
Steve0x2a authored Jan 28, 2022
1 parent fbc73de commit 3c2f7b7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions object/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/base64"
"fmt"
"strings"
"time"

"github.com/casdoor/casdoor/util"
"xorm.io/core"
Expand All @@ -45,6 +46,8 @@ type Token struct {
Scope string `xorm:"varchar(100)" json:"scope"`
TokenType string `xorm:"varchar(100)" json:"tokenType"`
CodeChallenge string `xorm:"varchar(100)" json:"codeChallenge"`
CodeIsUsed bool `json:"codeIsUsed"`
CodeExpireIn int64 `json:"codeExpireIn"`
}

type TokenWrapper struct {
Expand Down Expand Up @@ -119,6 +122,15 @@ func getTokenByCode(code string) *Token {
return nil
}

func updateUsedByCode(token *Token) bool {
affected, err := adapter.Engine.Where("code=?", token.Code).Cols("code_is_used").Update(token)
if err != nil {
panic(err)
}

return affected != 0
}

func GetToken(id string) *Token {
owner, name := util.GetOwnerAndNameFromId(id)
return getToken(owner, name)
Expand Down Expand Up @@ -238,6 +250,8 @@ func GetOAuthCode(userId string, clientId string, responseType string, redirectU
Scope: scope,
TokenType: "Bearer",
CodeChallenge: challenge,
CodeIsUsed: false,
CodeExpireIn: time.Now().Add(time.Minute * 5).Unix(),
}
AddToken(token)

Expand Down Expand Up @@ -311,7 +325,29 @@ func GetOAuthToken(grantType string, clientId string, clientSecret string, code
Scope: "",
}
}
if token.CodeIsUsed {
//Resist replay attacks, if the code is reused, the token generated with this code will be deleted
DeleteToken(token)
return &TokenWrapper{
AccessToken: "error: code has been used.",
TokenType: "",
ExpiresIn: 0,
Scope: "",
}
}
if time.Now().Unix() > token.CodeExpireIn {
//can only use the code to generate a token within five minutes
DeleteToken(token)
return &TokenWrapper{
AccessToken: "error: code has expired",
TokenType: "",
ExpiresIn: 0,
Scope: "",
}
}

token.CodeIsUsed = true
updateUsedByCode(token)
tokenWrapper := &TokenWrapper{
AccessToken: token.AccessToken,
IdToken: token.AccessToken,
Expand Down

0 comments on commit 3c2f7b7

Please sign in to comment.