forked from woodpecker-ci/woodpecker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance token checking (woodpecker-ci#3842)
(cherry picked from commit b8b6efb)
- Loading branch information
Showing
5 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package token_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/franela/goblin" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"go.woodpecker-ci.org/woodpecker/v2/shared/token" | ||
) | ||
|
||
func TestToken(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
|
||
g := goblin.Goblin(t) | ||
g.Describe("Token", func() { | ||
jwtSecret := "secret-to-sign-the-token" | ||
|
||
g.It("should parse a valid token", func() { | ||
_token := token.New(token.UserToken) | ||
_token.Set("user-id", "1") | ||
signedToken, err := _token.Sign(jwtSecret) | ||
assert.NoError(g, err) | ||
|
||
parsed, err := token.Parse([]token.Type{token.UserToken}, signedToken, func(_ *token.Token) (string, error) { | ||
return jwtSecret, nil | ||
}) | ||
|
||
assert.NoError(g, err) | ||
assert.NotNil(g, parsed) | ||
assert.Equal(g, "1", parsed.Get("user-id")) | ||
}) | ||
|
||
g.It("should fail to parse a token with a wrong type", func() { | ||
_token := token.New(token.UserToken) | ||
_token.Set("user-id", "1") | ||
signedToken, err := _token.Sign(jwtSecret) | ||
assert.NoError(g, err) | ||
|
||
_, err = token.Parse([]token.Type{token.AgentToken}, signedToken, func(_ *token.Token) (string, error) { | ||
return jwtSecret, nil | ||
}) | ||
|
||
assert.ErrorIs(g, err, jwt.ErrInvalidType) | ||
}) | ||
|
||
g.It("should fail to parse a token with a wrong secret", func() { | ||
_token := token.New(token.UserToken) | ||
_token.Set("user-id", "1") | ||
signedToken, err := _token.Sign(jwtSecret) | ||
assert.NoError(g, err) | ||
|
||
_, err = token.Parse([]token.Type{token.UserToken}, signedToken, func(_ *token.Token) (string, error) { | ||
return "this-is-a-wrong-secret", nil | ||
}) | ||
|
||
assert.ErrorIs(g, err, jwt.ErrSignatureInvalid) | ||
}) | ||
}) | ||
} |