#Key must be a PEM encoded PKCS1 or PKCS8 key

29 messages · Page 1 of 1 (latest)

pearl void
#

Im loading the keys from env file, but there seems to be no reason for them to not be working

#

ACCESS_TOKEN_PRIVATE_KEY=MIICWwIBAAKBgF3vNF+UbDmmZDlY2H+kUHkxivqtJLKUFBk4MCE1k/sZsWFjCfqcKj8RrVGx3aIBgW6lE52eIllwv56j/zUVZZbZxvWD3K4vnUUAValJH8Y5yLakT+bSKBajSrPs1NTd159Ndbme9fjEHvku0mjXyDZDt57etDq4WY1+NohN8ZzfAgMBAAECgYAsnDFvQvmy10a5xWvsEoj1RYYPTrMi7BgvcfPJkbxZf/YxnetbTVby5N4+8g2jprtlZJhQPo1plJYsLQ5Ck4rO6fv84RT8hE5asegA48f5uYd9X+vYnMTqQB5smj3MVX20Z2K0h/Lp485Efuv1fCgXfvABeTyldjJNGI0ks7O5QQJBAJ+RkenykO26nO6qHKnweOZpvmg2YUour/eFY0imefvCde5VoOWkCB23vtr+kSikp7b4sPYSNNJJtmvIWS7tbJkCQQCWs4oefSJYqXNTvs5b7cBmWDvqj9P2FITuWyvrMZ+3l1V7fjndrgv31MBrtatVRjQ+11aWoIUzacvcwELJ7og3AkEAmLPZPFYqhaZJWkinJ6X+PRpC4j3wOuqU8T3QNo1CPUDqVMvTLxAbyDy4Q6DMT2XLLlmuF75ymC98iIbxeewyOQJAIIRa8Bs2FwVxIgfQxRs24Z52RzWDmdxxlnDLuDL78IRR6JSi7cdJQ89/OX54mqzMRyb1wwid1Ssl1tsRTjsO3wJAcWA2FffgU+vTIBldDvZeEsF0sN300rxhq610D/yXaQz1jXqn049NLooehcpj55u6vQ+1XDTfgxV2qSdPH8bL6A==
ACCESS_TOKEN_PUBLIC_KEY=MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgF3vNF+UbDmmZDlY2H+kUHkxivqtJLKUFBk4MCE1k/sZsWFjCfqcKj8RrVGx3aIBgW6lE52eIllwv56j/zUVZZbZxvWD3K4vnUUAValJH8Y5yLakT+bSKBajSrPs1NTd159Ndbme9fjEHvku0mjXyDZDt57etDq4WY1+NohN8ZzfAgMBAAE=

#
func CreateToken(ttl time.Duration, payload interface{}, privateKey string) (string, error) {
    decodedPrivateKey, err := base64.StdEncoding.DecodeString(privateKey)
    if err != nil {
        return "", fmt.Errorf("could not decode key: %w", err)
    }

    key, err := jwt.ParseRSAPrivateKeyFromPEM(decodedPrivateKey)
    if err != nil {
        return "", fmt.Errorf("create: parse key: %w", err)
    }
}
#

it errors here jwt.ParseRSAPrivateKeyFromPEM

shut sail
#

the header and footer required
-----BEGIN XXX-----
-----END XXX-----
if the function that ask for it, ask for file path or raw []byte.

pearl void
#

What about the new lines?

shut sail
pearl void
#

illegal base64 data at input byte 0

shut sail
#

what jwt library do you use?

pearl void
#

golang-jwt

shut sail
#

full module name

#

just golang-jwt says nothing

pearl void
shut sail
#

you dont need ecodedPrivateKey, err := base64.StdEncoding.DecodeString(privateKey)

pearl void
#

just cast it?

shut sail
#

yeah

pearl void
#

key, err := jwt.ParseRSAPrivateKeyFromPEM([]byte("-----BEGIN RSA PRIVATE KEY-----" + privateKey + "-----END RSA PRIVATE KEY-----"))

#

Still getting the initial error

shut sail
#

line break after the header angd before the footer is required.

pearl void
#

ahhh

#

worked

shut sail
#

the line break is part of the header and footer .

---BEGIN XXX---\n <- header
content here does no need line break
\n---END xxx--- <-footer
pearl void
#

ye just did that

#

i thought it would handle them for me

#

thanks for the help

shut sail
#

pem file may contain multiple certificates. therefore, the footer is required.