diff --git a/OAuthSwiftTests/OAuth2SwiftTests.swift b/OAuthSwiftTests/OAuth2SwiftTests.swift index f9a00879..348adc50 100644 --- a/OAuthSwiftTests/OAuth2SwiftTests.swift +++ b/OAuthSwiftTests/OAuth2SwiftTests.swift @@ -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 ?? "" } @@ -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() { diff --git a/OAuthSwiftTests/OAuthSwiftCredentialTests.swift b/OAuthSwiftTests/OAuthSwiftCredentialTests.swift index 54a5ce50..d64f562f 100644 --- a/OAuthSwiftTests/OAuthSwiftCredentialTests.swift +++ b/OAuthSwiftTests/OAuthSwiftCredentialTests.swift @@ -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" @@ -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)") + } + } + } diff --git a/OAuthSwiftTests/TestServer.swift b/OAuthSwiftTests/TestServer.swift index 12bd809e..22370603 100644 --- a/OAuthSwiftTests/TestServer.swift +++ b/OAuthSwiftTests/TestServer.swift @@ -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" @@ -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)) } } diff --git a/Sources/OAuthSwiftClient.swift b/Sources/OAuthSwiftClient.swift index d44e0f9a..c465b8e4 100644 --- a/Sources/OAuthSwiftClient.swift +++ b/Sources/OAuthSwiftClient.swift @@ -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 } diff --git a/Sources/OAuthSwiftCredential.swift b/Sources/OAuthSwiftCredential.swift index cbbbfa90..ddde6bb7 100644 --- a/Sources/OAuthSwiftCredential.swift +++ b/Sources/OAuthSwiftCredential.swift @@ -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 @@ -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" } @@ -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 @@ -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) @@ -258,6 +263,7 @@ open class OAuthSwiftCredential: NSObject, NSSecureCoding, Codable { case oauthTokenSecret case oauthVerifier case oauthTokenExpiresAt + case idToken case version case signatureMethodRawValue } @@ -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) @@ -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 { @@ -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 }