Skip to content

Commit

Permalink
chore: apple secret gen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
activescott committed Jan 31, 2021
1 parent 46466d1 commit ed335e3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/scripts/ec_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIKsqu3EEoLbVnrv15zNx+KhjdUgoXhSvXmRON5H5aKB2oAoGCCqGSM49
AwEHoUQDQgAEAopbqqW7FTpow1J/03yo1rNdfCunyI9UMmYmKY1D7WrNbCXF2E7B
eMIsSWXd+BFJzY2+vE+J6aQtGAy8XeJBLQ==
-----END EC PRIVATE KEY-----
29 changes: 29 additions & 0 deletions server/scripts/gen-apple-private-key-cert-for-testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env sh

# Apple's secrets (as used in src/shared/lambda/oauth/apple.ts) use a cert downloaded from apple. This script generates a fake one for testing purposes

# first generate a private key:

##### openssl path
# MAC SPECIFIC :/
# NOTE: We need the non-mac bundled openssl for -addext (https://security.stackexchange.com/a/183973/40848)
# We're using homebrew installed v1.1 here and I tested with OpenSSL 1.1.1g 21 Apr 2020
OPEN_SSL_BIN=/usr/local/opt/openssl\@1.1/bin/openssl
#####

ALGORITH=secp256r1
echo "key:"
openssl ecparam -out ec_key.pem -name $ALGORITH -genkey

echo ""

echo "cert:"

$OPEN_SSL_BIN req\
-new\
-key ec_key.pem\
-x509\
-nodes
-days 3650\
-subj '/CN=test-apple-cert.com/O=No Company/C=US'
-out cert.pem
30 changes: 30 additions & 0 deletions server/src/shared/lambda/oauth/apple.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { appleSecret } from "./apple"

const APPLE_TEST_KEY = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIKsqu3EEoLbVnrv15zNx+KhjdUgoXhSvXmRON5H5aKB2oAoGCCqGSM49
AwEHoUQDQgAEAopbqqW7FTpow1J/03yo1rNdfCunyI9UMmYmKY1D7WrNbCXF2E7B
eMIsSWXd+BFJzY2+vE+J6aQtGAy8XeJBLQ==
-----END EC PRIVATE KEY-----
`
const teamID = "ABCDEFGHIJ"
const clientID = "com.test.foo-service-id"
const keyID = "LMNOPQRSTU"
const privateKey = APPLE_TEST_KEY

describe("apple", () => {
it("should return a valid jwt", () => {
const secret = appleSecret(teamID, clientID, keyID, privateKey)

expect(secret).toBeTruthy()
expect(typeof secret).toEqual("string")
expect(secret.length).toBeGreaterThan(10)
// jwts have three parts split by period:
const parts = secret.split(".")
expect(parts).toHaveLength(3)
})

it("should reject invalid args", () => {
expect(() => appleSecret(teamID, clientID, "", privateKey)).toThrowError()
expect(() => appleSecret(teamID, clientID, keyID, "")).toThrowError()
})
})

0 comments on commit ed335e3

Please sign in to comment.