Skip to content

Commit

Permalink
Module to generate JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
Exerra committed Oct 21, 2021
1 parent 3bd580e commit c4a4aa7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
26 changes: 26 additions & 0 deletions modules/createJWT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const jwt = require('jsonwebtoken')

/**
*
* @param {Object} credentials Apple Music credentials. Consists of a key containing MusicKit privileges, the team ID of developer account and the ID of the key
* @param {string} credentials.key A valid key generated from developer console that has MusicKit permissions
* @param {string} credentials.keyId ID of the credentials.key
* @param {string} credentials.teamId ID of the team that credentials.key belongs to
*/
const createJWT = (credentials) => {
if (!credentials || !credentials.key || !credentials.teamId || !credentials.keyId) {
throw new Error("No credentials supplied")
}

return jwt.sign({}, credentials.key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: credentials.teamId,
header: {
alg: 'ES256',
kid: credentials.keyId
}
});
}

module.exports.createJWT = createJWT
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-musickit-api",
"version": "2.1.0",
"version": "2.1.1",
"description": "A wrapper for the Apple Music API written in NodeJS",
"main": "index.js",
"repository": {
Expand Down
12 changes: 3 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const jwt = require('jsonwebtoken')
const axios = require('axios')

const { createJWT } = require("../../modules/createJWT")

const rootPath = "https://api.music.apple.com/v1"
let token
let auth
Expand All @@ -21,15 +23,7 @@ class MusicKit {
throw new Error("No credentials supplied")
}

token = jwt.sign({}, credentials.key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: credentials.teamId,
header: {
alg: 'ES256',
kid: credentials.keyId
}
});
token = createJWT(credentials)

this.updateAuth(token)
}
Expand Down
11 changes: 2 additions & 9 deletions src/personalized/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jwt = require('jsonwebtoken')
const axios = require('axios')
const { createJWT } = require("../../modules/createJWT")

const rootPath = "https://api.music.apple.com/v1"
let token
Expand Down Expand Up @@ -29,15 +30,7 @@ class MusicKit {
throw new Error("No credentials supplied")
}

token = jwt.sign({}, credentials.key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: credentials.teamId,
header: {
alg: 'ES256',
kid: credentials.keyId
}
});
token = createJWT(credentials)

this.updateAuth(token, credentials.userToken)
}
Expand Down
11 changes: 2 additions & 9 deletions src/promises/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jwt = require('jsonwebtoken')
const axios = require('axios')
const { createJWT } = require("../../modules/createJWT")

const rootPath = "https://api.music.apple.com/v1"
let token
Expand All @@ -18,15 +19,7 @@ class MusicKit {
throw new Error("No credentials supplied")
}

token = jwt.sign({}, credentials.key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: credentials.teamId,
header: {
alg: 'ES256',
kid: credentials.keyId
}
});
token = createJWT(credentials)

this.updateAuth(token)
}
Expand Down

0 comments on commit c4a4aa7

Please sign in to comment.