util.go
func peekField(buf []byte, fieldName string, pos tokenPosition) (string, error) {
	var err error

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

	// determine position decode
	var typ string
	var b []byte
	switch pos {
	case tokenPositionHeader:
		typ = "header"
		b = ut.Header
	case tokenPositionPayload:
		typ = "payload"
		b = ut.Payload

	default:
		return "", fmt.Errorf("invalid field %d", pos)
	}

	// b64 decode
	dec, err := b64.DecodeString(string(b))
	if err != nil {
		return "", fmt.Errorf("could not decode token %s", typ)
	}

	// json decode
	m := make(map[string]interface{})
	err = json.Unmarshal(dec, &m)
	if err != nil {
		return "", err
	}

	if val, ok := m[fieldName]; ok {
		return fmt.Sprintf("%v", val), nil
	}

	return "", fmt.Errorf("token %s field %s not present or invalid", typ, fieldName)
}
func peekField(buf []byte, fieldName string, pos tokenPosition) (string, error) {
	var err error

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

	// determine position decode
	var typ string
	var b []byte
	switch pos {
	case tokenPositionHeader:
		typ = "header"
		b = ut.Header
	case tokenPositionPayload:
		typ = "payload"
		b = ut.Payload

	default:
		return "", fmt.Errorf("invalid field %d", pos)
	}

	// b64 decode
	dec, err := b64.DecodeString(string(b))
	if err != nil {
		return "", fmt.Errorf("could not decode token %s", typ)
	}

	// json decode
	m := make(map[string]interface{})
	err = json.Unmarshal(dec, &m)
	if err != nil {
		return "", err
	}

	if val, ok := m[fieldName]; ok {
		return fmt.Sprintf("%v", val), nil
	}

	return "", fmt.Errorf("token %s field %s not present or invalid", typ, fieldName)
}
func peekField ( buf [ ] byte , fieldName string , pos tokenPosition ) ( string , error ) { var err error // split token ut := UnverifiedToken { } err = DecodeUnverifiedToken ( buf , & ut ) if err != nil { return " " , err } // determine position decode var typ string var b [ ] byte switch pos { case tokenPositionHeader : typ = " " b = ut . Header case tokenPositionPayload : typ = " " b = ut . Payload default : return " " , fmt . Errorf ( " " , pos ) } // b64 decode dec , err := b64 . DecodeString ( string ( b ) ) if err != nil { return " " , fmt . Errorf ( " " , typ ) } // json decode m := make ( map [ string ] interface { } ) err = json . Unmarshal ( dec , & m ) if err != nil { return " " , err } if val , ok := m [ fieldName ] ; ok { return fmt . Sprintf ( " " , val ) , nil } return " " , fmt . Errorf ( " " , typ , fieldName ) }
// peekField looks at an undecoded JWT, JSON decoding the data at pos, and
// returning the specified field's value as string.
//
// If the fieldName is not present, then an error will be returned.
peekField looks at an undecoded JWT JSON decoding the data at pos and returning the specified field s value as string . If the fieldName is not present then an error will be returned .