Skip to content

Commit

Permalink
Merge pull request #1 from colinhumber/id_token
Browse files Browse the repository at this point in the history
Id token
  • Loading branch information
SteeveFong authored Feb 11, 2023
2 parents a04f7b7 + a755516 commit c42cd55
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions OAuthSwiftTests/OAuth2SwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class OAuth2SwiftTests: XCTestCase {
let expectation = self.expectation(description: "request should succeed")

var state = ""

if case .code(_, let extractedState) = response {
state = extractedState ?? ""
}
Expand All @@ -90,6 +91,10 @@ class OAuth2SwiftTests: XCTestCase {
waitForExpectations(timeout: DefaultTimeout, handler: nil)

XCTAssertEqual(oauth.client.credential.oauthToken, server.oauth_token)

if case .code = response {
XCTAssertEqual(oauth.client.credential.idToken, self.server.id_token)
}
}

func testJSON_Error_Failure() {
Expand Down
26 changes: 26 additions & 0 deletions OAuthSwiftTests/OAuthSwiftCredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class OAuthSwiftCredentialTests: XCTestCase {
XCTAssertEqual(credential, object)
}

func testNSCodingWithIdToken() {
let credential = OAuthSwiftCredential(consumerKey: "foo", consumerSecret: "bar")
credential.oauthToken = "token"
credential.oauthTokenSecret = "secret"
credential.idToken = "idToken"
let data = NSKeyedArchiver.archivedData(withRootObject: credential)

let object = NSKeyedUnarchiver.unarchiveObject(with: data) as? OAuthSwiftCredential
XCTAssertEqual(credential, object)
}

func testCoding() {
let credential = OAuthSwiftCredential(consumerKey: "foo", consumerSecret: "bar")
credential.oauthToken = "token"
Expand All @@ -49,4 +60,19 @@ class OAuthSwiftCredentialTests: XCTestCase {
}
}

func testCodingWithIdToken() {
let credential = OAuthSwiftCredential(consumerKey: "foo", consumerSecret: "bar")
credential.oauthToken = "token"
credential.oauthTokenSecret = "secret"
credential.idToken = "idToken"

do {
let data = try JSONEncoder().encode(credential)
let object = try JSONDecoder().decode(OAuthSwiftCredential.self, from: data)
XCTAssertEqual(credential, object)
} catch {
XCTFail("Failed to encode or decode credential \(error)")
}
}

}
5 changes: 3 additions & 2 deletions OAuthSwiftTests/TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TestServer {

let oauth_token = "accesskey"
let oauth_token_secret = "accesssecret"
let id_token = "idtoken"
let valid_key = "key"
let valid_secret = "key"

Expand Down Expand Up @@ -82,9 +83,9 @@ class TestServer {

switch self.accessReturnType {
case .json:
return .ok(.json(["access_token":self.oauth_token] as AnyObject))
return .ok(.json(["access_token":self.oauth_token, "id_token":self.id_token] as AnyObject))
case .data:
return .ok(.text("access_token=\(self.oauth_token)" as String))
return .ok(.text("access_token=\(self.oauth_token)&id_token=\(self.id_token)" as String))
}

}
Expand Down
4 changes: 4 additions & 0 deletions Sources/OAuthSwiftClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ open class OAuthSwiftClient: NSObject {
return
}

if let idToken = responseParameters["id_token"] as? String {
this.credential.idToken = idToken.safeStringByRemovingPercentEncoding
}

if let refreshToken = responseParameters["refresh_token"] as? String {
this.credential.oauthRefreshToken = refreshToken.safeStringByRemovingPercentEncoding
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/OAuthSwiftCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
open var oauthRefreshToken = ""
open var oauthTokenSecret = ""
open var oauthTokenExpiresAt: Date?
open var idToken: String?
open internal(set) var oauthVerifier = ""
open var version: Version = .oauth1
open var signatureMethod: SignatureMethod = .HMAC_SHA1
Expand Down Expand Up @@ -149,6 +150,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
static let oauthTokenExpiresAt = base + "oauth_token_expires_at"
static let oauthTokenSecret = base + "oauth_token_secret"
static let oauthVerifier = base + "oauth_verifier"
static let idToken = base + "id_token"
static let version = base + "version"
static let signatureMethod = base + "signatureMethod"
}
Expand Down Expand Up @@ -224,6 +226,8 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {

self.oauthTokenExpiresAt = decoder
.decodeObject(of: NSDate.self, forKey: NSCodingKeys.oauthTokenExpiresAt) as Date?
self.idToken = decoder
.decodeObject(of: NSString.self, forKey: NSCodingKeys.idToken) as String?
self.version = Version(decoder.decodeInt32(forKey: NSCodingKeys.version))
if case .oauth1 = version {
self.signatureMethod = SignatureMethod(rawValue: (decoder.decodeObject(of: NSString.self, forKey: NSCodingKeys.signatureMethod) as String?) ?? "HMAC_SHA1") ?? .HMAC_SHA1
Expand All @@ -240,6 +244,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
coder.encode(self.oauthTokenSecret, forKey: NSCodingKeys.oauthTokenSecret)
coder.encode(self.oauthVerifier, forKey: NSCodingKeys.oauthVerifier)
coder.encode(self.oauthTokenExpiresAt, forKey: NSCodingKeys.oauthTokenExpiresAt)
coder.encode(self.idToken, forKey: NSCodingKeys.idToken)
coder.encode(self.version.toInt32, forKey: NSCodingKeys.version)
if case .oauth1 = version {
coder.encode(self.signatureMethod.rawValue, forKey: NSCodingKeys.signatureMethod)
Expand All @@ -258,6 +263,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
case oauthTokenSecret
case oauthVerifier
case oauthTokenExpiresAt
case idToken
case version
case signatureMethodRawValue
}
Expand All @@ -271,6 +277,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
try container.encode(self.oauthTokenSecret, forKey: .oauthTokenSecret)
try container.encode(self.oauthVerifier, forKey: .oauthVerifier)
try container.encodeIfPresent(self.oauthTokenExpiresAt, forKey: .oauthTokenExpiresAt)
try container.encodeIfPresent(self.idToken, forKey: .idToken)
try container.encode(self.version, forKey: .version)
if case .oauth1 = version {
try container.encode(self.signatureMethod.rawValue, forKey: .signatureMethodRawValue)
Expand All @@ -292,6 +299,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
self.oauthTokenSecret = try container.decode(type(of: self.oauthTokenSecret), forKey: .oauthTokenSecret)
self.oauthVerifier = try container.decode(type(of: self.oauthVerifier), forKey: .oauthVerifier)
self.oauthTokenExpiresAt = try container.decodeIfPresent(Date.self, forKey: .oauthTokenExpiresAt)
self.idToken = try container.decodeIfPresent(String.self, forKey: .idToken)
self.version = try container.decode(type(of: self.version), forKey: .version)

if case .oauth1 = version {
Expand Down Expand Up @@ -437,6 +445,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable {
&& lhs.oauthTokenSecret == rhs.oauthTokenSecret
&& lhs.oauthTokenExpiresAt == rhs.oauthTokenExpiresAt
&& lhs.oauthVerifier == rhs.oauthVerifier
&& lhs.idToken == rhs.idToken
&& lhs.version == rhs.version
&& lhs.signatureMethod == rhs.signatureMethod
}
Expand Down

0 comments on commit c42cd55

Please sign in to comment.