So I'm trying to authenticate the Firebase REST API. I'm using the Vapor framework for server side swift and I installed the JWT package.
I'm trying to use the data in the serviceAccountKey.json
file and JWT to generate an auth token.
Here is the code I've tried:
let payload = try JSON(node: [
"iat": Date().timeIntervalSince1970,
"exp": Date().timeIntervalSince1970 + 3600,
"iss": "client_email from serviceAccountKey.json",
"aud": "https://accounts.google.com/o/oauth2/token",
"scope": [
"https://www.googleapis.com/auth/firebase.database",
"https://www.googleapis.com/auth/userinfo.email"
]
])
let privateKey = "copied from serviceAccountKey.json"
let signer = try HS256(bytes: privateKey.bytes)
let jwt = try JWT(payload: payload, signer: signer)
let token = try jwt.createToken()
print(token)
serviceAccountKey.json
{
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ""
}
At this time I am using Xcode 8.3.3. Package.swift contains:
If you generate a service account credential you need to have in mind the following, taken from
https://cloud.google.com/storage/docs/authentication
: You can create a private key in the Cloud Platform Console by creating an OAuth Client ID for a service account. You can get your private key in JSON and PKCS12 format:JSON keys are required if you are using Application Default Credentials in a production environment outside of Google Cloud Platform. JSON keys cannot be converted to other formats. PKCS12 (.p12) is supported by many different programming languages and libraries. If needed, you can convert the key into other formats using OpenSSL (see Converting the private key to other formats). However, PKCS12 keys cannot be converted to JSON format.
Note: You do NOT need to generate a service account at console.cloud.google.com . Just follow the steps 1...6 listed below.
Go to https://console.firebase.google.com , click on your project, next to Overview click on the wheel Settings, click on Service Accounts, scroll to the bottom of the page and click on Generate New Private Key.
Convert the p.12 (a.k.a pkcs12) file to .pem (a.k.a pkcs1) using OpenSSL
cat /path/to/xxxx-privatekey.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > /path/to/secret.pem
Go to github and search VaporJWT and import it in Xcode. It will help you create a signed JSON Web Token.
On this github page you will learn how to extract the private key for RSA use.
Convert .pem to der
openssl rsa -in /path/to/secret.pem -outform der -out /path/to/private.der
openssl base64 -in /path/to/private.der -out /path/to/Desktop/private.txt
In private.txt you have the private key encoded in base64 which you can finally use to sign your JWT. Then you can make calls to Google API with the signed JWT.
``
If you just want to get things working, better off using version 1.5.0
And using the legacy secret. Project Settings>Service Accounts>Database Secrets