Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rawValue accessor for Claims #69

Merged
merged 1 commit into from Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -88,4 +88,7 @@ Carthage/Build
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/test_output

# Playground Timeline
timeline.xctimeline
5 changes: 3 additions & 2 deletions JWTDecode.playground/Contents.swift
Expand Up @@ -9,7 +9,7 @@ import JWTDecode
/*:
Then paste here the token you wish to decode
*/
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2fQ.LvF9wSheCB5xarpydmurWgi9NOZkdES5AbNb_UWk9Ew"
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2LCJlbWFpbCI6InVzZXJAaG9zdC5jb20iLCJjdXN0b20iOlsxLDIsM119.JeMRyHLkcoiqGxd958B6PABKNvhOhIgw-kbjecmhR_E"
//: You can generate a new token in [jwt.io](http://jwt.io)

/*:
Expand Down Expand Up @@ -44,11 +44,12 @@ do {
jwt.issuedAt
//: "exp" (Expiration Time)
jwt.expiresAt

//: ### Custom Claims
//: If we also have our custom claims we can retrive them calling `claim<T>(name: String) -> T?` where `T` is the value type of the claim, e.g.: a `String`

_ = jwt.claim(name: "email").string

_ = jwt.claim(name: "custom").rawValue as? [Int]
//: ### Error Handling
//: If the token is invalid an `NSError` will be thrown
} catch let error as NSError {
Expand Down
146 changes: 0 additions & 146 deletions JWTDecode.playground/timeline.xctimeline

This file was deleted.

5 changes: 5 additions & 0 deletions JWTDecode/JWTDecode.swift
Expand Up @@ -79,6 +79,11 @@ public struct Claim {
/// raw value of the claim
let value: Any?

/// original claim value
public var rawValue: Any? {
return self.value
}

/// value of the claim as `String`
public var string: String? {
return self.value as? String
Expand Down
18 changes: 18 additions & 0 deletions JWTDecodeTests/JWTDecodeSpec.swift
Expand Up @@ -209,6 +209,24 @@ class JWTDecodeSpec: QuickSpec {
expect(unknownClaim.double).to(beNil())
expect(unknownClaim.date).to(beNil())
}

context("raw claim") {

var token: JWT!

beforeEach {
token = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2LCJlbWFpbCI6InVzZXJAaG9zdC5jb20iLCJjdXN0b20iOlsxLDIsM119.JeMRyHLkcoiqGxd958B6PABKNvhOhIgw-kbjecmhR_E")
}

it("should return email") {
expect(token.claim(name: "email").string) == "user@host.com"
}

it("should return array") {
expect(token.claim(name: "custom").rawValue as? [Int]).toNot(beNil())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can go further and add a test for object-like claims and even check their values. For this line in particular, you can check the contents are [1,2,3]

}

}
}
}
}
Expand Down