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

Add redirect URL to iOS login - new scheme changes #664

Merged
merged 4 commits into from
Jul 7, 2023
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
4 changes: 2 additions & 2 deletions android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public String getName() {
}

@ReactMethod
public void webAuth(String scheme, String state, String nonce, String audience, String scope, String connection, int maxAge, String organization, String invitationUrl, int leeway, boolean ephemeralSession, ReadableMap additionalParameters, Promise promise) {
public void webAuth(String scheme, String redirectUri, String state, String nonce, String audience, String scope, String connection, int maxAge, String organization, String invitationUrl, int leeway, boolean ephemeralSession, ReadableMap additionalParameters, Promise promise) {
Map<String,String> cleanedParameters = new HashMap<>();
for (Map.Entry<String, Object> entry : additionalParameters.toHashMap().entrySet()) {
if (entry.getValue() != null) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public void onFailure(@NonNull AuthenticationException error) {
}

@ReactMethod
public void webAuthLogout(String scheme, boolean federated, Promise promise) {
public void webAuthLogout(String scheme, boolean federated, String redirectUri, Promise promise) {
WebAuthProvider.LogoutBuilder builder = WebAuthProvider.logout(this.auth0)
.withScheme(scheme);
if(federated) {
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ android {
namespace "com.auth0example"
defaultConfig {
applicationId "com.auth0example"
manifestPlaceholders = [auth0Domain: "brucke.auth0.com", auth0Scheme: "${applicationId}"]
manifestPlaceholders = [auth0Domain: "brucke.auth0.com", auth0Scheme: "${applicationId}.auth0"]
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down
13 changes: 13 additions & 0 deletions example/ios/Auth0Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>CFBundleURLName</key>
<string>auth0</string>
<key>CFBundleURLSchemes</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).auth0</string>
</array>
</dict>
</array>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
8 changes: 4 additions & 4 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ - (dispatch_queue_t)methodQueue
[self.nativeBridge enableLocalAuthenticationWithTitle:title cancelTitle:cancelTitle fallbackTitle:fallbackTitle evaluationPolicy: evaluationPolicy];
}

RCT_EXPORT_METHOD(webAuth:(NSString *)scheme state:(NSString *)state nonce:(NSString *)nonce audience:(NSString *)audience scope:(NSString *)scope connection:(NSString *)connection maxAge:(NSInteger)maxAge organization:(NSString *)organization invitationUrl:(NSString *)invitationUrl leeway:(NSInteger)leeway ephemeralSession:(BOOL)ephemeralSession additionalParameters:(NSDictionary *)additionalParameters resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.nativeBridge webAuthWithState:state nonce:nonce audience:audience scope:scope connection:connection maxAge:maxAge organization:organization invitationUrl:invitationUrl leeway:leeway ephemeralSession:ephemeralSession additionalParameters:additionalParameters resolve:resolve reject:reject];
RCT_EXPORT_METHOD(webAuth:(NSString *)scheme redirectUri:(NSString *)redirectUri state:(NSString *)state nonce:(NSString *)nonce audience:(NSString *)audience scope:(NSString *)scope connection:(NSString *)connection maxAge:(NSInteger)maxAge organization:(NSString *)organization invitationUrl:(NSString *)invitationUrl leeway:(NSInteger)leeway ephemeralSession:(BOOL)ephemeralSession additionalParameters:(NSDictionary *)additionalParameters resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.nativeBridge webAuthWithState:state redirectUri:redirectUri nonce:nonce audience:audience scope:scope connection:connection maxAge:maxAge organization:organization invitationUrl:invitationUrl leeway:leeway ephemeralSession:ephemeralSession additionalParameters:additionalParameters resolve:resolve reject:reject];
}

RCT_EXPORT_METHOD(webAuthLogout:(NSString *)scheme federated:(BOOL)federated resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.nativeBridge webAuthLogoutWithFederated:federated resolve:resolve reject:reject];
RCT_EXPORT_METHOD(webAuthLogout:(NSString *)scheme federated:(BOOL)federated redirectUri:(NSString *)redirectUri resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.nativeBridge webAuthLogoutWithFederated:federated redirectUri:redirectUri resolve:resolve reject:reject];
}

- (NSDictionary *)constantsToExport {
Expand Down
10 changes: 8 additions & 2 deletions ios/NativeBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public class NativeBridge: NSObject {
super.init()
}

@objc public func webAuth(state: String?, nonce: String?, audience: String?, scope: String?, connection: String?, maxAge: Int, organization: String?, invitationUrl: String?, leeway: Int, ephemeralSession: Bool, additionalParameters: [String: String], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
@objc public func webAuth(state: String?, redirectUri: String, nonce: String?, audience: String?, scope: String?, connection: String?, maxAge: Int, organization: String?, invitationUrl: String?, leeway: Int, ephemeralSession: Bool, additionalParameters: [String: String], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let builder = Auth0.webAuth(clientId: self.clientId, domain: self.domain)
if let value = URL(string: redirectUri) {
let _ = builder.redirectURL(value)
}
if let value = state {
let _ = builder.state(value)
}
Expand Down Expand Up @@ -83,8 +86,11 @@ public class NativeBridge: NSObject {

}

@objc public func webAuthLogout(federated: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
@objc public func webAuthLogout(federated: Bool, redirectUri: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let builder = Auth0.webAuth(clientId: self.clientId, domain: self.domain)
if let value = URL(string: redirectUri) {
let _ = builder.redirectURL(value)
}
builder.clearSession(federated: federated) { result in
switch result {
case .success:
Expand Down
7 changes: 6 additions & 1 deletion src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type Auth0Module = {
bundleIdentifier: string;
webAuth: (
scheme: string,
redirectUri: string,
state?: string,
nonce?: string,
audience?: string,
Expand All @@ -73,7 +74,11 @@ export type Auth0Module = {
ephemeralSession?: boolean,
additionalParameters?: { [key: string]: string }
) => Promise<Credentials>;
webAuthLogout: (scheme: string, federated: boolean) => Promise<void>;
webAuthLogout: (
scheme: string,
federated: boolean,
redirectUri: string
) => Promise<void>;
saveCredentials: (credentials: Credentials) => Promise<void>;
getCredentials: (
scope?: string,
Expand Down
10 changes: 9 additions & 1 deletion src/webauth/__tests__/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Agent', () => {
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'test',
'test://test.com/test-os/com.my.app/callback',
'state',
'nonce',
'audience',
Expand Down Expand Up @@ -132,7 +133,7 @@ describe('Agent', () => {
}
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith('test', true);
expect(mockLogin).toBeCalledWith('test', true, 'test://test.com/test-os/com.my.app/callback');
});
});

Expand Down Expand Up @@ -160,4 +161,11 @@ describe('Agent', () => {
await expect(agent.getScheme(true)).toEqual('com.test');
});
});


describe('callbackUri', () => {
it('should return callback uri with given domain and scheme', async () => {
await expect(agent.callbackUri('domain', 'scheme')).toEqual("scheme://domain/test-os/com.test/callback");
});
});
});
24 changes: 15 additions & 9 deletions src/webauth/agent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
NativeModules,
Linking,
Platform,
EmitterSubscription,
} from 'react-native';
import { NativeModules, Platform } from 'react-native';
import { Credentials } from 'src/types';
import { _ensureNativeModuleIsInitialized } from '../utils/nativeHelper';
import {
Expand Down Expand Up @@ -35,8 +30,10 @@ class Agent {
options.useLegacyCallbackUrl ?? false,
options.customScheme
);
let redirectUri = this.callbackUri(parameters.domain, scheme);
return A0Auth0.webAuth(
scheme,
redirectUri,
options.state,
options.nonce,
options.audience,
Expand All @@ -47,7 +44,7 @@ class Agent {
options.invitationUrl,
options.leeway ?? 0,
options.ephemeralSession ?? false,
options.additionalParameters
options.additionalParameters ?? {}
);
}

Expand All @@ -67,22 +64,31 @@ class Agent {
options.useLegacyCallbackUrl ?? false,
options.customScheme
);
let redirectUri = this.callbackUri(parameters.domain, scheme);
await _ensureNativeModuleIsInitialized(
NativeModules.A0Auth0,
parameters.clientId,
parameters.domain
);

return A0Auth0.webAuthLogout(scheme, federated);
return A0Auth0.webAuthLogout(scheme, federated, redirectUri);
}

getScheme(useLegacyCustomSchemeBehaviour: boolean, customScheme?: string) {
private getScheme(
useLegacyCustomSchemeBehaviour: boolean,
customScheme?: string
) {
let scheme = NativeModules.A0Auth0.bundleIdentifier.toLowerCase();
if (!useLegacyCustomSchemeBehaviour) {
scheme = scheme + '.auth0';
}
return customScheme ?? scheme;
}

private callbackUri(domain: string, scheme: string) {
let bundleIdentifier = NativeModules.A0Auth0.bundleIdentifier.toLowerCase();
return `${scheme}://${domain}/${Platform.OS}/${bundleIdentifier}/callback`;
}
}

export default Agent;