-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from enniosousa/master
feat: Add support to OIDC PEM certificate
- Loading branch information
Showing
7 changed files
with
238 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,20 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDTTCCAjWgAwIBAgIUF fUW7F4N/YDl/Ok6o5ogVKstmgwDQYJKoZIhvcNAQEL | ||
BQAwNTEzMDEGA1UEAwwqb2lkYy5mZWRlcmF0ZWQtc2lnbm9uLmNsb3VkLXRhc2tz | ||
LWVtdWxhdG9yMCAXDTI0MDYyNTIwNTMwNloYDzIwNTExMTExMjA1MzA2WjA1MTMw | ||
MQYDVQQDDCpvaWRjLmZlZGVyYXRlZC1zaWdub24uY2xvdWQtdGFza3MtZW11bGF0 | ||
b3IwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC EePjNlISDurX4F1J | ||
vNKK 2afgRYX89kgXuAAf7iKqbu/bYw37bC eak0tAb/4t4nkzf2QMda3Z6LccSz | ||
E/FsR54dHMKbbcCBcMZOSO5RReLsY/WdCZZxmfJyQPSOvyRk7vz2lq5yTrUa dCG | ||
12XiY/ckIJc8jR0m9uSvvqeL6EyeOkHsbIKESCUgCuyFM0/CEeb7ozRzhHe5W/NB | ||
Sm4TsIRyKw0fW7wczRo6dApdhzjZrc/jKWWkPvSM23TTxK1fLIgjA3gsVP37m8z0 | ||
WsESljiT0QCCBTZHsUSh2eTLp7yCs9XZvTPZ5Eu7iOAhM8zPLKphzotxwQ yf31e | ||
qQXXAgMBAAGjUzBRMB0GA1UdDgQWBBQM0EC S8XGgwTe9cqXmJFGEaiuzjAfBgNV | ||
HSMEGDAWgBQM0EC S8XGgwTe9cqXmJFGEaiuzjAPBgNVHRMBAf8EBTADAQH/MA0G | ||
CSqGSIb3DQEBCwUAA4IBAQAf36NX9kdGdBwQppY9lO5ElcxVbGg8RG8ieOFM86eg | ||
1TJ14I8tKBdR2wPd/N diRhsnctrVGEXulgItGvZjIKjnoWwVi/sPte5WJcMoR3Y | ||
csiLHBCW9tL6O8NaZuchSoKxlkE/qk2R1QLZtBaGXOjKm1 vIhNzNcdrMKinIfze | ||
OqbKJ0UDNapy59o65Eix8gZoeIc70WICWn3yKHcAah7FIKAVw2yA11QyuYa2xB9h | ||
1SuHbUN8voSaFaNdF3GIHxktLB7UU1yz6WDxbz1dBmWNK7FxyeeWrtjBKikQDZZu | ||
hxrkYARnT2CySwuUk5IvxzTYeebRoBQzGD8SuTqIvCzZ | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC EePjNlISDurX | ||
4F1JvNKK 2afgRYX89kgXuAAf7iKqbu/bYw37bC eak0tAb/4t4nkzf2QMda3Z6L | ||
ccSzE/FsR54dHMKbbcCBcMZOSO5RReLsY/WdCZZxmfJyQPSOvyRk7vz2lq5yTrUa | ||
dCG12XiY/ckIJc8jR0m9uSvvqeL6EyeOkHsbIKESCUgCuyFM0/CEeb7ozRzhHe5 | ||
W/NBSm4TsIRyKw0fW7wczRo6dApdhzjZrc/jKWWkPvSM23TTxK1fLIg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1DwgFx6vYqXS4sA | ||
0/5Z8k0qBgj L55/MNFyvnBbUJBOsd1DkxY19wXIjQavStF9UezhjQImbp2SXj6j | ||
SJDbKywlMOPOW78Rk KhkXCMvloywCvavGxMYropAoGBAK7ECAs0AZLlUPkXuYmL | ||
U1GzFKUl3xDgczMSof5nPJCHcUm0fl02883IhEFEBvzqo5fu8pIzKGKpVwrNud7E | ||
/cLTJUkejD5e0h4V5ykcTUs9yDrxopQ54NW0lj7Se00e5MAUH0SRwbjbFdzQ3AYd | ||
FSkhEKj2YXWlriv3hyPIC8Aq | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 2,14 @@ package main | |
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/json" | ||
"encoding/pem" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -113,6 118,80 @@ func TestOpenIdJWKSHttpHandler(t *testing.T) { | |
) | ||
} | ||
|
||
func TestOpenIdCertsHttpHandler(t *testing.T) { | ||
OpenIDConfig.KeyID = "any-key-id" | ||
|
||
resp := performRequest("GET", "/certs", openIDCertsHttpHandler) | ||
|
||
assert.Equal(t, http.StatusOK, resp.Code) | ||
|
||
var err error | ||
|
||
expires, err := time.Parse(http.TimeFormat, resp.Result().Header.Get("Expires")) | ||
require.NoError(t, err) | ||
assertRoughTimestamp(t, 24*time.Hour, expires.Unix(), "Expect future expires") | ||
|
||
openIdcert, err := os.ReadFile("oidc.cert") | ||
require.NoError(t, err) | ||
|
||
certs := map[string]interface{}{ | ||
OpenIDConfig.KeyID: string(openIdcert), | ||
} | ||
|
||
certsJSON, err := json.Marshal(certs) | ||
require.NoError(t, err) | ||
assert.JSONEq(t, string(certsJSON), resp.Body.String()) | ||
} | ||
|
||
func TestValidateOIDCTokenWithCertPem(t *testing.T) { | ||
var err error | ||
|
||
tokenStr := createOIDCToken("[email protected]", "http://my.service/foo?bar=v", "http://my.api") | ||
|
||
openIdcert, err := os.ReadFile("oidc.cert") | ||
require.NoError(t, err) | ||
|
||
parsePEMCert := func(pemCert []byte) (*rsa.PublicKey, error) { | ||
block, _ := pem.Decode(pemCert) | ||
if block == nil || block.Type != "CERTIFICATE" { | ||
return nil, fmt.Errorf("failed to decode PEM block containing certificate") | ||
} | ||
|
||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse certificate: %v", err) | ||
} | ||
|
||
rsaPub, ok := cert.PublicKey.(*rsa.PublicKey) | ||
if !ok { | ||
return nil, fmt.Errorf("not an RSA public key") | ||
} | ||
|
||
return rsaPub, nil | ||
} | ||
|
||
// Load the public key | ||
pubKey, err := parsePEMCert(openIdcert) | ||
require.NoError(t, err) | ||
|
||
// Parse the token | ||
parser := new(jwt.Parser) | ||
_, _, err = parser.ParseUnverified(tokenStr, &jwt.MapClaims{}) | ||
require.NoError(t, err) | ||
|
||
// Verify the token | ||
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) { | ||
// Validate the algorithm - this is optional but recommended | ||
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
return pubKey, nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
assert.True(t, token.Valid, "Token is valid") | ||
} | ||
|
||
func TestConfigureOpenIdIssuerRejectsInvalidUrl(t *testing.T) { | ||
var err error | ||
_, err = configureOpenIdIssuer("junk") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,27 @@ | ||
You can create you own private key and self-signed certificate using OpenSSL to use in you own emulator. Here's how you can do it: | ||
|
||
1. **Generate a Private Key:** | ||
Use OpenSSL to generate a new private key file. Here’s how you can do it: | ||
|
||
```bash | ||
openssl genpkey -algorithm RSA -out oidc.key | ||
``` | ||
|
||
This command generates an RSA private key and saves it to `oidc.key`. | ||
|
||
2. **Generate a Self-Signed Certificate:** | ||
Once you have the private key, you can generate a self-signed certificate using the `req` command, as you've shown: | ||
|
||
```bash | ||
openssl req -new -x509 -key oidc.key -out oidc.cert -days 10000 -subj "/CN=oidc.federated-signon.cloud-tasks-emulator" -config "path/to/openssl.cnf" | ||
``` | ||
|
||
- `-new`: Generate a new certificate request. | ||
- `-x509`: Create a self-signed certificate. | ||
- `-key oidc.key`: Use the private key file `oidc.key`. | ||
- `-out oidc.cert`: Output the certificate to `oidc.cert`. | ||
- `-days 10000`: Validity of the certificate in days. | ||
- `-subj "/CN=oidc.federated-signon.cloud-tasks-emulator"`: Subject of the certificate. Adjust the Common Name (CN) as needed. | ||
- `-config "path/to/openssl.cnf"`: Path to your OpenSSL configuration file. Adjust this path according to your setup. | ||
|
||
Make sure to replace `"path/to/openssl.cnf"` with the actual path to your OpenSSL configuration file on your system. This configuration file typically contains settings like default certificate extensions and other parameters relevant to certificate generation. Adjust the CN (Common Name) parameter (`/CN=`) to match your specific domain or server name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters