Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no need for string slice and call to strings.join #115

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,34 @@ func NewWithClaims(method SigningMethod, claims Claims) *Token {
// SignedString creates and returns a complete, signed JWT. The token is signed
// using the SigningMethod specified in the token.
func (t *Token) SignedString(key interface{}) (string, error) {
var sig, sstr string
var err error
if sstr, err = t.SigningString(); err != nil {
sstr, err := t.SigningString()
if err != nil {
return "", err
}
if sig, err = t.Method.Sign(sstr, key); err != nil {

sig, err := t.Method.Sign(sstr, key)
if err != nil {
return "", err
}
return strings.Join([]string{sstr, sig}, "."), nil

return sstr + "." + sig, nil
}

// SigningString generates the signing string. This is the most expensive part
// of the whole deal. Unless you need this for something special, just go
// straight for the SignedString.
func (t *Token) SigningString() (string, error) {
var err error
var jsonValue []byte

if jsonValue, err = json.Marshal(t.Header); err != nil {
h, err := json.Marshal(t.Header)
if err != nil {
return "", err
}
header := EncodeSegment(jsonValue)

if jsonValue, err = json.Marshal(t.Claims); err != nil {
c, err := json.Marshal(t.Claims)
if err != nil {
return "", err
}
claim := EncodeSegment(jsonValue)

return strings.Join([]string{header, claim}, "."), nil
return EncodeSegment(h) + "." + EncodeSegment(c), nil
}

// Parse parses, validates, verifies the signature and returns the parsed token.
Expand Down