Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package jws provides a partial implementation of JSON Web Signature encoding and decoding. It exists to support the golang.org/x/oauth2 package. See RFC 7515. Deprecated: this package is not intended for public use and might be removed in the future. It exists for internal use only. Please switch to another JWS package or copy this package into your own source tree.
package jws // import "golang.org/x/oauth2/jws"

import (
	
	
	
	
	
	
	
	
	
	
	
)
ClaimSet contains information about the JWT signature including the permissions being requested (scopes), the target of the token, the issuer, the time the token was issued, and the lifetime of the token.
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).
Email for which the application is requesting delegated access (Optional).
	Sub string `json:"sub,omitempty"`
The old name of Sub. Client keeps setting Prn to be complaint with legacy OAuth 2.0 providers. (Optional)
	Prn string `json:"prn,omitempty"`
See http://tools.ietf.org/html/draft-jones-json-web-token-10#section-4.3 This array is marshalled using custom code (see (c *ClaimSet) encode()).
	PrivateClaims map[string]interface{} `json:"-"`
}

Reverting time back for machines whose time is not perfectly in sync. If client machine's time is in the future according to Google servers, an access token will not be issued.
	 := 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
	}
Marshal private claim set and then append it to b.
	,  := json.Marshal(.PrivateClaims)
	if  != nil {
		return "", fmt.Errorf("jws: invalid map of private claims %v", .PrivateClaims)
	}
Concatenate public and private claim JSON objects.
	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
}
Header represents the header for the signed JWS payloads.
The algorithm used for signature.
	Algorithm string `json:"alg"`
Represents the token type.
	Typ string `json:"typ"`
The optional hint of which key is being used.
	KeyID string `json:"kid,omitempty"`
}

func ( *Header) () (string, error) {
	,  := json.Marshal()
	if  != nil {
		return "", 
	}
	return base64.RawURLEncoding.EncodeToString(), nil
}
Decode decodes a claim set from a JWS payload.
decode returned id token to get expiry
	 := strings.Split(, ".")
TODO(jbd): Provide more context about the error.
		return nil, errors.New("jws: invalid token received")
	}
	,  := base64.RawURLEncoding.DecodeString([1])
	if  != nil {
		return nil, 
	}
	 := &ClaimSet{}
	 = json.NewDecoder(bytes.NewBuffer()).Decode()
	return , 
}
Signer returns a signature for the given data.
type Signer func(data []byte) (sig []byte, err error)
EncodeWithSigner encodes a header and claim set with the provided signer.
func ( *Header,  *ClaimSet,  Signer) (string, error) {
	,  := .encode()
	if  != nil {
		return "", 
	}
	,  := .encode()
	if  != nil {
		return "", 
	}
	 := fmt.Sprintf("%s.%s", , )
	,  := ([]byte())
	if  != nil {
		return "", 
	}
	return fmt.Sprintf("%s.%s", , base64.RawURLEncoding.EncodeToString()), nil
}
Encode encodes a signed JWS with provided header and claim set. This invokes EncodeWithSigner using crypto/rsa.SignPKCS1v15 with the given RSA private key.
func ( *Header,  *ClaimSet,  *rsa.PrivateKey) (string, error) {
	 := func( []byte) ( []byte,  error) {
		 := sha256.New()
		.Write()
		return rsa.SignPKCS1v15(rand.Reader, , crypto.SHA256, .Sum(nil))
	}
	return EncodeWithSigner(, , )
}
Verify tests whether the provided JWT token's signature was produced by the private key associated with the supplied public key.
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())