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

chore: kickoff release #3901

Merged
merged 2 commits into from
Oct 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ShowHostedUISignIn: NSObject, Action {

guard let callbackURL = URL(string: hostedUIConfig.oauth.signInRedirectURI),
let callbackURLScheme = callbackURL.scheme else {
let event = SignInEvent(eventType: .throwAuthError(.hostedUI(.signInURI)))
let event = HostedUIEvent(eventType: .throwError(.hostedUI(.signInURI)))
logVerbose("\(#fileID) Sending event \(event)", environment: environment)
await dispatcher.send(event)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class AuthConfigureOperation: ConfigureOperation {
override public func main() {
if isCancelled {
finish()
dispatch(result: .failure(AuthError.configuration(
"Configuration operation was cancelled",
"", nil)))
return
}

Expand All @@ -51,6 +54,7 @@ class AuthConfigureOperation: ConfigureOperation {
for await state in stateSequences {
if case .configured = state {
finish()
dispatch(result: .success(()))
break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extension HostedUISignInState {

case .showingUI:
if case .throwError(let error) = event.isHostedUIEvent {
// Remove this?
let action = CancelSignIn()
return .init(newState: .error(error), actions: [action])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,45 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
}
}

/// Test that the Auth plugin emits `InternalConfigureAuth` that is used by the Logging Category
///
/// - Given: Given a valid config
/// - When:
/// - I configure auth with the given configuration
/// - Then:
/// - I should receive `InternalConfigureAuth` Hub event
///
func testEmittingInternalConfigureAuthHubEvent() throws {
let expectation = expectation(description: "conifguration should complete")
let subscription = Amplify.Hub.publisher(for: .auth).sink { payload in

if payload.eventName == "InternalConfigureAuth" {
expectation.fulfill()
}
}
let plugin = AWSCognitoAuthPlugin()
try Amplify.add(plugin: plugin)

let categoryConfig = AuthCategoryConfiguration(plugins: [
"awsCognitoAuthPlugin": [
"CredentialsProvider": [
"CognitoIdentity": [
"Default": [
"PoolId": "cc",
"Region": "us-east-1"
]
]
]
]
])
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
do {
try Amplify.configure(amplifyConfig)
} catch {
XCTFail("Should not throw error. \(error)")
}
wait(for: [expectation], timeout: 5.0)
subscription.cancel()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ class AWSAuthHostedUISignInTests: XCTestCase {
return URLSession(configuration: configuration)
}

private func customPlugin(with cusotmConfiguration: HostedUIConfigurationData?) -> AWSCognitoAuthPlugin {
let plugin = AWSCognitoAuthPlugin()
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
MockURLProtocol.requestHandler = { _ in
return (HTTPURLResponse(), self.mockJson)
}

func sessionFactory() -> HostedUISessionBehavior {
MockHostedUISession(result: mockHostedUIResult)
}

func mockRandomString() -> RandomStringBehavior {
return MockRandomStringGenerator(mockString: mockState, mockUUID: mockState)
}

let environment = BasicHostedUIEnvironment(configuration: cusotmConfiguration ?? configuration,
hostedUISessionFactory: sessionFactory,
urlSessionFactory: urlSessionMock,
randomStringFactory: mockRandomString)
let authEnvironment = Defaults.makeDefaultAuthEnvironment(
userPoolFactory: { self.mockIdentityProvider },
hostedUIEnvironment: environment)
let stateMachine = Defaults.authStateMachineWith(
environment: authEnvironment,
initialState: initialState
)

plugin.configure(
authConfiguration: Defaults.makeDefaultAuthConfigData(withHostedUI: configuration),
authEnvironment: authEnvironment,
authStateMachine: stateMachine,
credentialStoreStateMachine: Defaults.makeDefaultCredentialStateMachine(),
hubEventHandler: MockAuthHubEventBehavior(),
analyticsHandler: MockAnalyticsHandler())
return plugin
}

override func setUp() {
plugin = AWSCognitoAuthPlugin()
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
Expand Down Expand Up @@ -310,6 +347,41 @@ class AWSAuthHostedUISignInTests: XCTestCase {
await fulfillment(of: [expectation], timeout: networkTimeout)
}

@MainActor
func testInvalidRedirectConfigurationFailure() async {
let invalidRedirectConfig = HostedUIConfigurationData(clientId: "clientId", oauth: .init(
domain: "cognitodomain",
scopes: ["name"],
signInRedirectURI: "@#$%junk1343",
signOutRedirectURI: "@3451://"))
let testPlugin = customPlugin(with: invalidRedirectConfig)

mockHostedUIResult = .success([
.init(name: "state", value: mockState),
.init(name: "code", value: mockProof)
])
mockTokenResult = [
"refresh_token": AWSCognitoUserPoolTokens.testData.refreshToken,
"expires_in": 10] as [String: Any]
mockJson = try! JSONSerialization.data(withJSONObject: mockTokenResult)
MockURLProtocol.requestHandler = { _ in
return (HTTPURLResponse(), self.mockJson)
}

let expectation = expectation(description: "SignIn operation should complete")
do {
_ = try await testPlugin.signInWithWebUI(presentationAnchor: ASPresentationAnchor(), options: nil)
XCTFail("Should not succeed")
} catch {
guard case AuthError.configuration = error else {
XCTFail("Should not fail with error = \(error)")
return
}
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: networkTimeout)
}



/// Test a signIn restart while another sign in is in progress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ final class AWSCloudWatchLoggingCategoryClient {
enum CognitoEventName: String {
case signInAPI = "Auth.signInAPI"
case signOutAPI = "Auth.signOutAPI"
case configured = "InternalConfigureAuth"
}
switch payload.eventName {
case HubPayload.EventName.Auth.signedIn, CognitoEventName.signInAPI.rawValue:
case HubPayload.EventName.Auth.signedIn, CognitoEventName.signInAPI.rawValue, CognitoEventName.configured.rawValue:
takeUserIdentifierFromCurrentUser()
case HubPayload.EventName.Auth.signedOut, CognitoEventName.signOutAPI.rawValue:
self.userIdentifier = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ public class AWSCloudWatchLoggingPlugin: LoggingCategoryPlugin {
let localStore: LoggingConstraintsLocalStore = UserDefaults.standard
localStore.reset()
}

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
self.loggingClient.takeUserIdentifierFromCurrentUser()
}
}
}

Expand Down
Loading