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

	// grab encode targets
	headerObj, payloadObj, err := encodeTargets(alg, obj)
	if err != nil {
		return nil, err
	}

	// json encode header
	header, err := json.Marshal(headerObj)
	if err != nil {
		return nil, err
	}

	// b64 encode playload
	headerEnc := make([]byte, b64.EncodedLen(len(header)))
	b64.Encode(headerEnc, header)

	// json encode payload
	payload, err := json.Marshal(payloadObj)
	if err != nil {
		return nil, err
	}

	// b64 encode playload
	payloadEnc := make([]byte, b64.EncodedLen(len(payload)))
	b64.Encode(payloadEnc, payload)

	// allocate result
	var buf bytes.Buffer

	// add header
	_, err = buf.Write(headerEnc)
	if err != nil {
		return nil, err
	}

	// add 1st separator
	_, err = buf.Write(tokenSep)
	if err != nil {
		return nil, err
	}

	// add payload
	_, err = buf.Write(payloadEnc)
	if err != nil {
		return nil, err
	}

	// sign
	sig, err := signer.Sign(buf.Bytes())
	if err != nil {
		return nil, err
	}

	// add 2nd separator
	_, err = buf.Write(tokenSep)
	if err != nil {
		return nil, err
	}

	// add sig
	_, err = buf.Write(sig)
	if err != nil {
		return nil, err
	}

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

	// grab encode targets
	headerObj, payloadObj, err := encodeTargets(alg, obj)
	if err != nil {
		return nil, err
	}

	// json encode header
	header, err := json.Marshal(headerObj)
	if err != nil {
		return nil, err
	}

	// b64 encode playload
	headerEnc := make([]byte, b64.EncodedLen(len(header)))
	b64.Encode(headerEnc, header)

	// json encode payload
	payload, err := json.Marshal(payloadObj)
	if err != nil {
		return nil, err
	}

	// b64 encode playload
	payloadEnc := make([]byte, b64.EncodedLen(len(payload)))
	b64.Encode(payloadEnc, payload)

	// allocate result
	var buf bytes.Buffer

	// add header
	_, err = buf.Write(headerEnc)
	if err != nil {
		return nil, err
	}

	// add 1st separator
	_, err = buf.Write(tokenSep)
	if err != nil {
		return nil, err
	}

	// add payload
	_, err = buf.Write(payloadEnc)
	if err != nil {
		return nil, err
	}

	// sign
	sig, err := signer.Sign(buf.Bytes())
	if err != nil {
		return nil, err
	}

	// add 2nd separator
	_, err = buf.Write(tokenSep)
	if err != nil {
		return nil, err
	}

	// add sig
	_, err = buf.Write(sig)
	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
func Encode ( alg Algorithm , signer Signer , obj interface { } ) ( [ ] byte , error ) { var err error // grab encode targets headerObj , payloadObj , err := encodeTargets ( alg , obj ) if err != nil { return nil , err } // json encode header header , err := json . Marshal ( headerObj ) if err != nil { return nil , err } // b64 encode playload headerEnc := make ( [ ] byte , b64 . EncodedLen ( len ( header ) ) ) b64 . Encode ( headerEnc , header ) // json encode payload payload , err := json . Marshal ( payloadObj ) if err != nil { return nil , err } // b64 encode playload payloadEnc := make ( [ ] byte , b64 . EncodedLen ( len ( payload ) ) ) b64 . Encode ( payloadEnc , payload ) // allocate result var buf bytes . Buffer // add header _ , err = buf . Write ( headerEnc ) if err != nil { return nil , err } // add 1st separator _ , err = buf . Write ( tokenSep ) if err != nil { return nil , err } // add payload _ , err = buf . Write ( payloadEnc ) if err != nil { return nil , err } // sign sig , err := signer . Sign ( buf . Bytes ( ) ) if err != nil { return nil , err } // add 2nd separator _ , err = buf . Write ( tokenSep ) if err != nil { return nil , err } // add sig _ , err = buf . Write ( sig ) if err != nil { return nil , err } return buf . Bytes ( ) , nil }
// Encode encodes a JWT using the Algorithm and Signer, returning the URL-safe
// encoded token or any errors encountered during encoding.
Encode encodes a JWT using the Algorithm and Signer returning the URL - safe encoded token or any errors encountered during encoding .