remove string slice and strings.join (#115)

This commit is contained in:
Mones Zarrugh 2023-02-22 04:28:00 +02:00 committed by GitHub
parent 148d710109
commit 0d2f0d4809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 13 deletions

View File

@ -63,35 +63,34 @@ func NewWithClaims(method SigningMethod, claims Claims) *Token {
// SignedString creates and returns a complete, signed JWT. The token is signed // SignedString creates and returns a complete, signed JWT. The token is signed
// using the SigningMethod specified in the token. // using the SigningMethod specified in the token.
func (t *Token) SignedString(key interface{}) (string, error) { func (t *Token) SignedString(key interface{}) (string, error) {
var sig, sstr string sstr, err := t.SigningString()
var err error if err != nil {
if sstr, err = t.SigningString(); err != nil {
return "", err 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 "", err
} }
return strings.Join([]string{sstr, sig}, "."), nil
return sstr + "." + sig, nil
} }
// SigningString generates the signing string. This is the most expensive part // SigningString generates the signing string. This is the most expensive part
// of the whole deal. Unless you need this for something special, just go // of the whole deal. Unless you need this for something special, just go
// straight for the SignedString. // straight for the SignedString.
func (t *Token) SigningString() (string, error) { func (t *Token) SigningString() (string, error) {
var err error h, err := json.Marshal(t.Header)
var jsonValue []byte if err != nil {
if jsonValue, err = json.Marshal(t.Header); err != nil {
return "", err 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 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. // Parse parses, validates, verifies the signature and returns the parsed token.