Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit c4a4aa7

Browse files
committed
Module to generate JWT
1 parent 3bd580e commit c4a4aa7

5 files changed

Lines changed: 34 additions & 28 deletions

File tree

modules/createJWT.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const jwt = require('jsonwebtoken')
2+
3+
/**
4+
*
5+
* @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
6+
* @param {string} credentials.key A valid key generated from developer console that has MusicKit permissions
7+
* @param {string} credentials.keyId ID of the credentials.key
8+
* @param {string} credentials.teamId ID of the team that credentials.key belongs to
9+
*/
10+
const createJWT = (credentials) => {
11+
if (!credentials || !credentials.key || !credentials.teamId || !credentials.keyId) {
12+
throw new Error("No credentials supplied")
13+
}
14+
15+
return jwt.sign({}, credentials.key, {
16+
algorithm: 'ES256',
17+
expiresIn: '180d',
18+
issuer: credentials.teamId,
19+
header: {
20+
alg: 'ES256',
21+
kid: credentials.keyId
22+
}
23+
});
24+
}
25+
26+
module.exports.createJWT = createJWT

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-musickit-api",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "A wrapper for the Apple Music API written in NodeJS",
55
"main": "index.js",
66
"repository": {

src/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const jwt = require('jsonwebtoken')
22
const axios = require('axios')
33

4+
const { createJWT } = require("../../modules/createJWT")
5+
46
const rootPath = "https://api.music.apple.com/v1"
57
let token
68
let auth
@@ -21,15 +23,7 @@ class MusicKit {
2123
throw new Error("No credentials supplied")
2224
}
2325

24-
token = jwt.sign({}, credentials.key, {
25-
algorithm: 'ES256',
26-
expiresIn: '180d',
27-
issuer: credentials.teamId,
28-
header: {
29-
alg: 'ES256',
30-
kid: credentials.keyId
31-
}
32-
});
26+
token = createJWT(credentials)
3327

3428
this.updateAuth(token)
3529
}

src/personalized/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const jwt = require('jsonwebtoken')
22
const axios = require('axios')
3+
const { createJWT } = require("../../modules/createJWT")
34

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

32-
token = jwt.sign({}, credentials.key, {
33-
algorithm: 'ES256',
34-
expiresIn: '180d',
35-
issuer: credentials.teamId,
36-
header: {
37-
alg: 'ES256',
38-
kid: credentials.keyId
39-
}
40-
});
33+
token = createJWT(credentials)
4134

4235
this.updateAuth(token, credentials.userToken)
4336
}

src/promises/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const jwt = require('jsonwebtoken')
22
const axios = require('axios')
3+
const { createJWT } = require("../../modules/createJWT")
34

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

21-
token = jwt.sign({}, credentials.key, {
22-
algorithm: 'ES256',
23-
expiresIn: '180d',
24-
issuer: credentials.teamId,
25-
header: {
26-
alg: 'ES256',
27-
kid: credentials.keyId
28-
}
29-
});
22+
token = createJWT(credentials)
3023

3124
this.updateAuth(token)
3225
}

0 commit comments

Comments
 (0)