jwt - GO - How to convert public key from string to PEM format -
i trying use jwt library jwt validation. , getting public key application calling rest endpoint returning public key in string format.
so when trying send public key in same string format, getting "invalid key format". on how convert string formatted key valid pem format great.
func (test *teststrategy) dojwtvalidation(token string, key string, logger *util.logger) (testresponse, error) { parsedtoken, jwterr := jwt.parse(token, func(token *jwt.token) (interface{}, error) { return decodedjwt.parsedpubkey, nil }) below error getting when passing key string jwt.pasrse() call.
public key: miibijanbgkqhkig9w0baqefaaocaq8amiibcgkcaqeasfwkb/esl6i3drvhaonw3dfy8enl0yapidzccoluyfbjn9zzir1wxmnmjfle1k89qhgg42wgwevtiwa1xftfouksziwsjf6fsczx5h56zyys/wwio3rwwynlfbszt+ga71+ndsu+a0dy7nn7zgp8krsu4um5ve7qqtrerniukpzscn1cgzufuqsddqmkwten8hh1mfx1mum54ngqwilmqxqdroyogmmxiaaakhabcmuipmulvvdvwujc9ssdsc/j05qczn3kkiebryiyb6zly2w7wfiv+db7/icponsysd0fxhwegnnbqtigonf9wzwtap+o8wmr9sb3gkgvnblvbqidaqab
that's pem encoded key, it's missing begin & end headers. key simple base64 encoded, can decode , unmarshal rsa key follows:
base64data := []byte(`miibijanbgkqhkig9w0baqefaaocaq8amiibcgkcaqeasfwkb/esl6i3drvhaonw3dfy8enl0yapidzccoluyfbjn9zzir1wxmnmjfle1k89qhgg42wgwevtiwa1xftfouksziwsjf6fsczx5h56zyys/wwio3rwwynlfbszt+ga71+ndsu+a0dy7nn7zgp8krsu4um5ve7qqtrerniukpzscn1cgzufuqsddqmkwten8hh1mfx1mum54ngqwilmqxqdroyogmmxiaaakhabcmuipmulvvdvwujc9ssdsc/j05qczn3kkiebryiyb6zly2w7wfiv+db7/icponsysd0fxhwegnnbqtigonf9wzwtap+o8wmr9sb3gkgvnblvbqidaqab`) d := make([]byte, base64.stdencoding.decodedlen(len(base64data))) n, err := base64.stdencoding.decode(d, base64data) if err != nil { // handle error } d = d[:n] key,err:=x509.parsepkixpublickey(d) if err != nil { // handle error } fmt.println(key) if need key in pem encoded form, simple add appropriate header , footer, e.g. -----begin public key----- & -----end public key-----. note begin header must start on own line , end in new line ("\n"). end header must proceeded new line.

Comments
Post a Comment