Skip to content

Instantly share code, notes, and snippets.

@cp-sumi-k
Last active June 8, 2022 10:38
Show Gist options
  • Save cp-sumi-k/9f68492cf66a9426e16dfbd1b3700ea3 to your computer and use it in GitHub Desktop.
Save cp-sumi-k/9f68492cf66a9426e16dfbd1b3700ea3 to your computer and use it in GitHub Desktop.
// prepare header structure contains algorithm and token type
type NotificationHeader struct {
Alg string `json:"alg"`
X5c []string `json:"x5c"`
}
// extract header from given JWS formatted token
func extractHeaderByIndex(tokenStr string, index int) ([]byte, error) {
tokenArr := strings.Split(tokenStr, ".") // get header from token
headerByte, err := base64.RawStdEncoding.DecodeString(tokenArr[0]) //convert header to byte
if err != nil {
return nil, err
}
var header NotificationHeader
err = json.Unmarshal(headerByte, &header) // bind byte to header structure
if err != nil {
return nil, err
}
certByte, err := base64.StdEncoding.DecodeString(header.X5c[index]) //decode x.509 cerificate headers to byte
if err != nil {
return nil, err
}
return certByte, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment