Skip to content

Sample for generating a MusicKit JSON web token in Swift

Notifications You must be signed in to change notification settings

klaas/SwiftJWTSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Version SwiftPM compatible Platform

SwiftJWTSample

Sample for generating a JSON Web Token with https://github.com/IBM-Swift/Swift-JWT.git

Usage:

swift run generateToken <team-id> <key-id> path_to_keys/AuthKey.p8

Sample invocation:

SwiftJWTSample$ swift run generateToken JJXPCWBWAJ G7HMGM8QA mykeys/AuthKey_G7HMGM8QA.p8

JSON Web Token:
"eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiIsImtpZCI6Ikc3SE1HTThRQSJ9.eyJpc3MiOiJKSlhQQ1dCV0FKIiwiaWF0IjoxNTYwNzY5MDg4LCJleHAiOjE1NjU5NTMwODh9.ychIfPAv9zEl3ko-SHSIq0ngbh6qwAoS6K7e4wwUT9iPY7f2lXlnsXjbs3a5UuUb16V3y1vcKE_hPvfm5-CJQg"

Header:
{"typ":"JWT","alg":"ES256","kid":"G7HMGM8QA"}
Payload:
{"iss":"JJXPCWBWAJ","iat":1560769088,"exp":1565953088}
Signature:
ychIfPAv9zEl3ko-SHSIq0ngbh6qwAoS6K7e4wwUT9iPY7f2lXlnsXjbs3a5UuUb16V3y1vcKE_hPvfm5-CJQg

Code

It's more or less only an invocation of the SwiftJWT API:

struct MyClaims: Claims {
	let iss: String
	let iat: Int
	let exp: Int
}

let myHeader = Header(kid: keyId)
let myClaims = MyClaims(iss: teamId, iat: Date(), exp: Date() +  24 * 60 * 60)
var myJWT = SwiftJWT.JWT(header: myHeader, claims: myClaims)

let token = try! myJWT.sign(using: .es256(privateKey: try! String(contentsOf: keyFileUrl).data(using: .utf8)!))

Reference

About

Sample for generating a MusicKit JSON web token in Swift

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages