Skip to content

Commit

Permalink
Moved out IAMAuthInterceptor to remove dependency on AWSCore
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Mar 9, 2020
1 parent b8b5809 commit 8f56c85
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 242 deletions.
8 changes: 0 additions & 8 deletions AppSyncRealTimeClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@
21D38B622409B94100EC2A8D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 21D38B602409B94100EC2A8D /* LaunchScreen.storyboard */; };
21D38B692409B95B00EC2A8D /* amplifyconfiguration.json in Resources */ = {isa = PBXBuildFile; fileRef = 21D38B4B2409B6C000EC2A8D /* amplifyconfiguration.json */; };
21D38B6D240A262800EC2A8D /* AppSyncJSONHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B6C240A262800EC2A8D /* AppSyncJSONHelper.swift */; };
21D38B7B240A2A1300EC2A8D /* IAMAuthInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B77240A2A1300EC2A8D /* IAMAuthInterceptor.swift */; };
21D38B7C240A2A1300EC2A8D /* OIDCAuthInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B78240A2A1300EC2A8D /* OIDCAuthInterceptor.swift */; };
21D38B83240A392B00EC2A8D /* APIKeyAuthInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B82240A392B00EC2A8D /* APIKeyAuthInterceptor.swift */; };
21D38B89240A39E400EC2A8D /* OIDCAuthInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B85240A39E400EC2A8D /* OIDCAuthInterceptorTests.swift */; };
21D38B8A240A39E400EC2A8D /* IAMAuthInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B86240A39E400EC2A8D /* IAMAuthInterceptorTests.swift */; };
21D38B8B240A39E400EC2A8D /* AppSyncJSONHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B87240A39E400EC2A8D /* AppSyncJSONHelperTests.swift */; };
21D38B8C240A39E400EC2A8D /* APIKeyAuthInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B88240A39E400EC2A8D /* APIKeyAuthInterceptorTests.swift */; };
21D38B8E240A3C2300EC2A8D /* ConnectionProviderFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D38B8D240A3C2300EC2A8D /* ConnectionProviderFactory.swift */; };
Expand Down Expand Up @@ -146,11 +144,9 @@
21D38B612409B94100EC2A8D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
21D38B632409B94100EC2A8D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
21D38B6C240A262800EC2A8D /* AppSyncJSONHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppSyncJSONHelper.swift; sourceTree = "<group>"; };
21D38B77240A2A1300EC2A8D /* IAMAuthInterceptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IAMAuthInterceptor.swift; sourceTree = "<group>"; };
21D38B78240A2A1300EC2A8D /* OIDCAuthInterceptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OIDCAuthInterceptor.swift; sourceTree = "<group>"; };
21D38B82240A392B00EC2A8D /* APIKeyAuthInterceptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIKeyAuthInterceptor.swift; sourceTree = "<group>"; };
21D38B85240A39E400EC2A8D /* OIDCAuthInterceptorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OIDCAuthInterceptorTests.swift; sourceTree = "<group>"; };
21D38B86240A39E400EC2A8D /* IAMAuthInterceptorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IAMAuthInterceptorTests.swift; sourceTree = "<group>"; };
21D38B87240A39E400EC2A8D /* AppSyncJSONHelperTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppSyncJSONHelperTests.swift; sourceTree = "<group>"; };
21D38B88240A39E400EC2A8D /* APIKeyAuthInterceptorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIKeyAuthInterceptorTests.swift; sourceTree = "<group>"; };
21D38B8D240A3C2300EC2A8D /* ConnectionProviderFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionProviderFactory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -411,7 +407,6 @@
isa = PBXGroup;
children = (
21D38B82240A392B00EC2A8D /* APIKeyAuthInterceptor.swift */,
21D38B77240A2A1300EC2A8D /* IAMAuthInterceptor.swift */,
21D38B78240A2A1300EC2A8D /* OIDCAuthInterceptor.swift */,
217F39C82406E98400F1A0B3 /* RealtimeGatewayURLInterceptor.swift */,
);
Expand All @@ -422,7 +417,6 @@
isa = PBXGroup;
children = (
21D38B85240A39E400EC2A8D /* OIDCAuthInterceptorTests.swift */,
21D38B86240A39E400EC2A8D /* IAMAuthInterceptorTests.swift */,
21D38B87240A39E400EC2A8D /* AppSyncJSONHelperTests.swift */,
21D38B88240A39E400EC2A8D /* APIKeyAuthInterceptorTests.swift */,
);
Expand Down Expand Up @@ -797,7 +791,6 @@
217F39D42406E98400F1A0B3 /* RealtimeConnectionProvider+Websocket.swift in Sources */,
217F39DC2406E98400F1A0B3 /* AppSyncSubscriptionConnection.swift in Sources */,
21D38B6D240A262800EC2A8D /* AppSyncJSONHelper.swift in Sources */,
21D38B7B240A2A1300EC2A8D /* IAMAuthInterceptor.swift in Sources */,
217F39CE2406E98400F1A0B3 /* ConnectionProviderError.swift in Sources */,
217F39E12406E98400F1A0B3 /* StarscreamAdapter.swift in Sources */,
217F39D72406E98400F1A0B3 /* RealtimeConnectionProvider+ConnectionInterceptable.swift in Sources */,
Expand All @@ -811,7 +804,6 @@
buildActionMask = 2147483647;
files = (
21D38B89240A39E400EC2A8D /* OIDCAuthInterceptorTests.swift in Sources */,
21D38B8A240A39E400EC2A8D /* IAMAuthInterceptorTests.swift in Sources */,
217F39F22406EA4000F1A0B3 /* RealtimeConnectionProviderTests.swift in Sources */,
217F39F02406EA4000F1A0B3 /* AppSyncSubscriptionConnectionTests.swift in Sources */,
21D38B8C240A39E400EC2A8D /* APIKeyAuthInterceptorTests.swift in Sources */,
Expand Down
13 changes: 10 additions & 3 deletions AppSyncRealTimeClient/Interceptor/APIKeyAuthInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import AWSCore

/// Auth interceptor for API Key based authentication
public class APIKeyAuthInterceptor: AuthInterceptor {
Expand Down Expand Up @@ -68,12 +67,20 @@ public class APIKeyAuthInterceptor: AuthInterceptor {

/// Authentication header for API key based auth
private class APIKeyAuthenticationHeader: AuthenticationHeader {
static let ISO8601DateFormat: String = "yyyyMMdd'T'HHmmss'Z'"
let date: String?
let apiKey: String

var formatter: DateFormatter = {
var formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = ISO8601DateFormat
return formatter
}()

init(apiKey: String, host: String) {
let amzDate = NSDate.aws_clockSkewFixed() as NSDate
self.date = amzDate.aws_stringValue(AWSDateISO8601DateFormat2)
self.date = formatter.string(from: Date())
self.apiKey = apiKey
super.init(host: host)
}
Expand Down
143 changes: 0 additions & 143 deletions AppSyncRealTimeClient/Interceptor/IAMAuthInterceptor.swift

This file was deleted.

22 changes: 11 additions & 11 deletions AppSyncRealTimeClient/Interceptor/OIDCAuthInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import AWSCore

public class OIDCAuthInterceptor: AuthInterceptor {

Expand All @@ -18,16 +17,16 @@ public class OIDCAuthInterceptor: AuthInterceptor {

public func interceptMessage(_ message: AppSyncMessage, for endpoint: URL) -> AppSyncMessage {
let host = endpoint.host!
var jwtToken: String?
authProvider.getLatestAuthToken { (token, error) in
let jwtToken: String
switch authProvider.getLatestAuthToken() {
case .success(let token):
jwtToken = token
}
guard let token = jwtToken else {
case .failure:
return message
}
switch message.messageType {
case .subscribe:
let authHeader = UserPoolsAuthenticationHeader(token: token, host: host)
let authHeader = UserPoolsAuthenticationHeader(token: jwtToken, host: host)
var payload = message.payload ?? AppSyncMessage.Payload()
payload.authHeader = authHeader

Expand All @@ -43,14 +42,15 @@ public class OIDCAuthInterceptor: AuthInterceptor {

public func interceptConnection(_ request: AppSyncConnectionRequest, for endpoint: URL) -> AppSyncConnectionRequest {
let host = endpoint.host!
var jwtToken: String?
authProvider.getLatestAuthToken { (token, error) in
let jwtToken: String
switch authProvider.getLatestAuthToken() {
case .success(let token):
jwtToken = token
}
guard let token = jwtToken else {
case .failure:
return request
}
let authHeader = UserPoolsAuthenticationHeader(token: token, host: host)

let authHeader = UserPoolsAuthenticationHeader(token: jwtToken, host: host)
let base64Auth = AppSyncJSONHelper.base64AuthenticationBlob(authHeader)

let payloadData = SubscriptionConstants.emptyPayload.data(using: .utf8)
Expand Down
4 changes: 2 additions & 2 deletions AppSyncRealTimeClient/Support/AppSyncJSONHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import Foundation

struct AppSyncJSONHelper {
public struct AppSyncJSONHelper {

static func base64AuthenticationBlob(_ header: AuthenticationHeader ) -> String {
public static func base64AuthenticationBlob(_ header: AuthenticationHeader ) -> String {
let jsonEncoder = JSONEncoder()
do {
let jsonHeader = try jsonEncoder.encode(header)
Expand Down
2 changes: 1 addition & 1 deletion AppSyncRealTimeClient/Support/OIDCAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
//

public protocol OIDCAuthProvider {
func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void)
func getLatestAuthToken() -> Result<String, Error>
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class OIDCAuthInterceptorTests: XCTestCase {
}

class MockUserPoolsAuthProvider: OIDCAuthProvider {

func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {
callback("jwtToken", nil)
func getLatestAuthToken() -> Result<String, Error> {
return .success("jwtToken")
}
}
7 changes: 3 additions & 4 deletions HostApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// AppDelegate.swift
// HostApp
// Copyright 2018-2020 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// Created by Law, Michael on 2/28/20.
// Copyright © 2020 amazonaws. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//

import UIKit
Expand Down
7 changes: 3 additions & 4 deletions HostApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//
// ContentView.swift
// HostApp
// Copyright 2018-2020 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// Created by Law, Michael on 2/28/20.
// Copyright © 2020 amazonaws. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//

import SwiftUI
Expand Down
Loading

0 comments on commit 8f56c85

Please sign in to comment.