Source File
jws.go
Belonging Package
golang.org/x/oauth2/jws
package jws // import "golang.org/x/oauth2/jws"
import (
)
type ClaimSet struct {
Iss string `json:"iss"` // email address of the client_id of the application making the access token request
Scope string `json:"scope,omitempty"` // space-delimited list of the permissions the application requests
Aud string `json:"aud"` // descriptor of the intended target of the assertion (Optional).
Exp int64 `json:"exp"` // the expiration time of the assertion (seconds since Unix epoch)
Iat int64 `json:"iat"` // the time the assertion was issued (seconds since Unix epoch)
Typ string `json:"typ,omitempty"` // token type (Optional).
PrivateClaims map[string]interface{} `json:"-"`
}
:= time.Now().Add(-10 * time.Second)
if .Iat == 0 {
.Iat = .Unix()
}
if .Exp == 0 {
.Exp = .Add(time.Hour).Unix()
}
if .Exp < .Iat {
return "", fmt.Errorf("jws: invalid Exp = %v; must be later than Iat = %v", .Exp, .Iat)
}
, := json.Marshal()
if != nil {
return "",
}
if len(.PrivateClaims) == 0 {
return base64.RawURLEncoding.EncodeToString(), nil
}
, := json.Marshal(.PrivateClaims)
if != nil {
return "", fmt.Errorf("jws: invalid map of private claims %v", .PrivateClaims)
}
if !bytes.HasSuffix(, []byte{'}'}) {
return "", fmt.Errorf("jws: invalid JSON %s", )
}
if !bytes.HasPrefix(, []byte{'{'}) {
return "", fmt.Errorf("jws: invalid JSON %s", )
}
[len()-1] = ',' // Replace closing curly brace with a comma.
= append(, [1:]...) // Append private claims.
return base64.RawURLEncoding.EncodeToString(), nil
}
return nil, errors.New("jws: invalid token received")
}
, := base64.RawURLEncoding.DecodeString([1])
if != nil {
return nil,
}
:= &ClaimSet{}
= json.NewDecoder(bytes.NewBuffer()).Decode()
return ,
}
func ( string, *rsa.PublicKey) error {
:= strings.Split(, ".")
if len() != 3 {
return errors.New("jws: invalid token received, token must have 3 parts")
}
:= [0] + "." + [1]
, := base64.RawURLEncoding.DecodeString([2])
if != nil {
return
}
:= sha256.New()
.Write([]byte())
return rsa.VerifyPKCS1v15(, crypto.SHA256, .Sum(nil), []byte())
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |