Skip to content

Commit 53fb73f

Browse files
authored
Merge pull request #40 from linuxfoundation/jme/LFXV2-1515
feat: support raw PEM in AUTH0_M2M_PRIVATE_BASE64_KEY
2 parents 491d761 + f392ee3 commit 53fb73f

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ The Auth0 integration can be configured using environment variables:
143143
- **If not set, defaults to `${AUTH0_TENANT}.auth0.com`**
144144
- `AUTH0_M2M_CLIENT_ID`: Auth0 Machine-to-Machine application client ID
145145
- **Required when using Auth0 repository type**
146-
- `AUTH0_M2M_PRIVATE_BASE64_KEY`: Base64-encoded private key for Auth0 M2M authentication
146+
- `AUTH0_M2M_PRIVATE_BASE64_KEY`: Private key for Auth0 M2M authentication (base64-encoded or raw PEM)
147147
- **Required when using Auth0 repository type**
148148
- `AUTH0_AUDIENCE`: Auth0 API audience/identifier for the Management API
149149
- **Required when using Auth0 repository type**

internal/infrastructure/auth0/impersonation.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"crypto/rsa"
99
"crypto/x509"
10-
"encoding/base64"
1110
"encoding/json"
1211
"encoding/pem"
1312
"fmt"
@@ -60,17 +59,17 @@ func NewImpersonationFlow(ctx context.Context, domain string) (port.Impersonator
6059
return nil, errors.NewUnexpected(constants.Auth0LFXv2APIAudienceEnvKey + " is required")
6160
}
6261

63-
privateKeyB64 := os.Getenv(constants.Auth0M2MPrivateBase64KeyEnvKey)
64-
if privateKeyB64 == "" {
62+
privateKeyRaw := os.Getenv(constants.Auth0M2MPrivateBase64KeyEnvKey)
63+
if privateKeyRaw == "" {
6564
return nil, errors.NewUnexpected(constants.Auth0M2MPrivateBase64KeyEnvKey + " is required")
6665
}
6766

68-
decoded, err := base64.StdEncoding.DecodeString(privateKeyB64)
67+
privateKeyPEM, err := decodePrivateKey(privateKeyRaw)
6968
if err != nil {
70-
return nil, errors.NewUnexpected("failed to base64-decode "+constants.Auth0M2MPrivateBase64KeyEnvKey, err)
69+
return nil, err
7170
}
7271

73-
rsaKey, err := parseRSAPrivateKey(decoded)
72+
rsaKey, err := parseRSAPrivateKey([]byte(privateKeyPEM))
7473
if err != nil {
7574
return nil, errors.NewUnexpected("failed to parse private key", err)
7675
}

internal/infrastructure/auth0/token.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log/slog"
1111
"net/http"
1212
"os"
13+
"strings"
1314
"time"
1415

1516
"github.com/auth0/go-auth0/authentication"
@@ -150,18 +151,15 @@ func loadM2MConfigFromEnv(ctx context.Context, config Config) (m2mConfig, error)
150151
return m2mConfig{}, errors.NewUnexpected(constants.Auth0AudienceEnvKey + " is required")
151152
}
152153

153-
// private key is base64 encoded
154-
privateKey := os.Getenv(constants.Auth0M2MPrivateBase64KeyEnvKey)
155-
if privateKey == "" {
154+
privateKeyRaw := os.Getenv(constants.Auth0M2MPrivateBase64KeyEnvKey)
155+
if privateKeyRaw == "" {
156156
return m2mConfig{}, errors.NewUnexpected(constants.Auth0M2MPrivateBase64KeyEnvKey + " is required")
157157
}
158158

159-
decoded, err := base64.StdEncoding.DecodeString(privateKey)
159+
privateKey, err := decodePrivateKey(privateKeyRaw)
160160
if err != nil {
161-
return m2mConfig{}, errors.NewUnexpected("failed to base64-decode "+constants.Auth0M2MPrivateBase64KeyEnvKey, err)
161+
return m2mConfig{}, err
162162
}
163-
privateKey = string(decoded)
164-
//
165163

166164
// Optional organization
167165
organization := os.Getenv("AUTH0_ORGANIZATION")
@@ -247,3 +245,17 @@ func NewProfileClientAuthConfig(ctx context.Context, domain string) (*authentica
247245

248246
return authConfig, nil
249247
}
248+
249+
// decodePrivateKey accepts a private key value that may be base64-encoded or raw PEM.
250+
// If the value is already a PEM key (starts with "-----BEGIN"), it is returned as-is.
251+
// Otherwise it is base64-decoded.
252+
func decodePrivateKey(raw string) (string, error) {
253+
if strings.HasPrefix(raw, "-----BEGIN") {
254+
return raw, nil
255+
}
256+
decoded, err := base64.StdEncoding.DecodeString(raw)
257+
if err != nil {
258+
return "", errors.NewUnexpected("failed to decode "+constants.Auth0M2MPrivateBase64KeyEnvKey+": value is neither valid PEM nor valid base64", err)
259+
}
260+
return string(decoded), nil
261+
}

pkg/constants/global.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const (
5151
// Auth0M2MClientIDEnvKey is the environment variable key for the Auth0 M2M client ID
5252
Auth0M2MClientIDEnvKey = "AUTH0_M2M_CLIENT_ID"
5353

54-
// Auth0M2MPrivateBase64KeyEnvKey is the environment variable key for the Auth0 M2M base64 encoded private key
54+
// Auth0M2MPrivateBase64KeyEnvKey is the environment variable key for the Auth0 M2M private key (base64-encoded or raw PEM)
5555
Auth0M2MPrivateBase64KeyEnvKey = "AUTH0_M2M_PRIVATE_BASE64_KEY"
5656

5757
// Auth0AudienceEnvKey is the environment variable key for the Auth0 audience

0 commit comments

Comments
 (0)