jwt.go
func Decode(alg Algorithm, signer Signer, buf []byte, obj interface{}) error {
	var err error

	// split token
	ut := UnverifiedToken{}
	err = DecodeUnverifiedToken(buf, &ut)
	if err != nil {
		return err
	}

	// verify signature
	sig, err := signer.Verify(buf[:len(ut.Header)+len(tokenSep)+len(ut.Payload)], ut.Signature)
	if err != nil {
		return ErrInvalidSignature
	}

	// b64 decode header
	headerBuf, err := b64.DecodeString(string(ut.Header))
	if err != nil {
		return err
	}

	// json decode header
	header := Header{}
	err = json.Unmarshal(headerBuf, &header)
	if err != nil {
		return err
	}

	// verify alg matches header algorithm
	if alg != header.Algorithm {
		return ErrInvalidAlgorithm
	}

	// set header in the provided obj
	err = decodeToObjOrFieldWithTag(headerBuf, obj, "header", &header)
	if err != nil {
		return err
	}

	// b64 decode payload
	payloadBuf, err := b64.DecodeString(string(ut.Payload))
	if err != nil {
		return err
	}

	// json decode payload
	payload := Claims{}
	err = json.Unmarshal(payloadBuf, &payload)
	if err != nil {
		return err
	}

	// set payload in the provided obj
	err = decodeToObjOrFieldWithTag(payloadBuf, obj, "payload", &payload)
	if err != nil {
		return err
	}

	// set sig in the provided obj
	field := getFieldWithTag(obj, "signature")
	if field != nil {
		field.Set(reflect.ValueOf(sig))
	}

	return nil
}
func Decode(alg Algorithm, signer Signer, buf []byte, obj interface{}) error {
	var err error

	// split token
	ut := UnverifiedToken{}
	err = DecodeUnverifiedToken(buf, &ut)
	if err != nil {
		return err
	}

	// verify signature
	sig, err := signer.Verify(buf[:len(ut.Header)+len(tokenSep)+len(ut.Payload)], ut.Signature)
	if err != nil {
		return ErrInvalidSignature
	}

	// b64 decode header
	headerBuf, err := b64.DecodeString(string(ut.Header))
	if err != nil {
		return err
	}

	// json decode header
	header := Header{}
	err = json.Unmarshal(headerBuf, &header)
	if err != nil {
		return err
	}

	// verify alg matches header algorithm
	if alg != header.Algorithm {
		return ErrInvalidAlgorithm
	}

	// set header in the provided obj
	err = decodeToObjOrFieldWithTag(headerBuf, obj, "header", &header)
	if err != nil {
		return err
	}

	// b64 decode payload
	payloadBuf, err := b64.DecodeString(string(ut.Payload))
	if err != nil {
		return err
	}

	// json decode payload
	payload := Claims{}
	err = json.Unmarshal(payloadBuf, &payload)
	if err != nil {
		return err
	}

	// set payload in the provided obj
	err = decodeToObjOrFieldWithTag(payloadBuf, obj, "payload", &payload)
	if err != nil {
		return err
	}

	// set sig in the provided obj
	field := getFieldWithTag(obj, "signature")
	if field != nil {
		field.Set(reflect.ValueOf(sig))
	}

	return nil
}
func Decode ( alg Algorithm , signer Signer , buf [ ] byte , obj interface { } ) error { var err error // split token ut := UnverifiedToken { } err = DecodeUnverifiedToken ( buf , & ut ) if err != nil { return err } // verify signature sig , err := signer . Verify ( buf [ : len ( ut . Header ) + len ( tokenSep ) + len ( ut . Payload ) ] , ut . Signature ) if err != nil { return ErrInvalidSignature } // b64 decode header headerBuf , err := b64 . DecodeString ( string ( ut . Header ) ) if err != nil { return err } // json decode header header := Header { } err = json . Unmarshal ( headerBuf , & header ) if err != nil { return err } // verify alg matches header algorithm if alg != header . Algorithm { return ErrInvalidAlgorithm } // set header in the provided obj err = decodeToObjOrFieldWithTag ( headerBuf , obj , " " , & header ) if err != nil { return err } // b64 decode payload payloadBuf , err := b64 . DecodeString ( string ( ut . Payload ) ) if err != nil { return err } // json decode payload payload := Claims { } err = json . Unmarshal ( payloadBuf , & payload ) if err != nil { return err } // set payload in the provided obj err = decodeToObjOrFieldWithTag ( payloadBuf , obj , " " , & payload ) if err != nil { return err } // set sig in the provided obj field := getFieldWithTag ( obj , " " ) if field != nil { field . Set ( reflect . ValueOf ( sig ) ) } return nil }
// Decode decodes a serialized JWT in buf into obj, and verifies the JWT
// signature using the Algorithm and Signer.
//
// If the token or signature is invalid, ErrInvalidToken or ErrInvalidSignature
// will be returned, respectively. Otherwise, any other errors encountered
// during token decoding will be returned.
Decode decodes a serialized JWT in buf into obj and verifies the JWT signature using the Algorithm and Signer . If the token or signature is invalid ErrInvalidToken or ErrInvalidSignature will be returned respectively . Otherwise any other errors encountered during token decoding will be returned .