forked from Mirrors/oauth2
oauth2/jws: minor readability improvements
Change-Id: I8bcf0bc88b3db49ac281531524e792e8547e1e07 Reviewed-on: https://go-review.googlesource.com/1671 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
7bbf2199a5
commit
7512128337
21
jws/jws.go
21
jws/jws.go
|
@ -104,7 +104,7 @@ func (h *Header) encode() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode decodes a claim set from a JWS payload.
|
// Decode decodes a claim set from a JWS payload.
|
||||||
func Decode(payload string) (c *ClaimSet, err error) {
|
func Decode(payload string) (*ClaimSet, error) {
|
||||||
// decode returned id token to get expiry
|
// decode returned id token to get expiry
|
||||||
s := strings.Split(payload, ".")
|
s := strings.Split(payload, ".")
|
||||||
if len(s) < 2 {
|
if len(s) < 2 {
|
||||||
|
@ -115,31 +115,28 @@ func Decode(payload string) (c *ClaimSet, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c = &ClaimSet{}
|
c := &ClaimSet{}
|
||||||
err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(c)
|
err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(c)
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode encodes a signed JWS with provided header and claim set.
|
// Encode encodes a signed JWS with provided header and claim set.
|
||||||
func Encode(header *Header, c *ClaimSet, signature *rsa.PrivateKey) (payload string, err error) {
|
func Encode(header *Header, c *ClaimSet, signature *rsa.PrivateKey) (string, error) {
|
||||||
var encodedHeader, encodedClaimSet string
|
head, err := header.encode()
|
||||||
encodedHeader, err = header.encode()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return "", err
|
||||||
}
|
}
|
||||||
encodedClaimSet, err = c.encode()
|
cs, err := c.encode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return "", err
|
||||||
}
|
}
|
||||||
|
ss := fmt.Sprintf("%s.%s", head, cs)
|
||||||
ss := fmt.Sprintf("%s.%s", encodedHeader, encodedClaimSet)
|
|
||||||
h := sha256.New()
|
h := sha256.New()
|
||||||
h.Write([]byte(ss))
|
h.Write([]byte(ss))
|
||||||
b, err := rsa.SignPKCS1v15(rand.Reader, signature, crypto.SHA256, h.Sum(nil))
|
b, err := rsa.SignPKCS1v15(rand.Reader, signature, crypto.SHA256, h.Sum(nil))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
sig := base64Encode(b)
|
sig := base64Encode(b)
|
||||||
return fmt.Sprintf("%s.%s", ss, sig), nil
|
return fmt.Sprintf("%s.%s", ss, sig), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue