Skip to content
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<h2 align="center">React Native App Auth</h2>
<p align="center">
<strong>React native bridge for AppAuth - an SDK for communicating with OAuth2 providers</strong>
</p>
<br><br>
<p align="center">
<img src="https://media.giphy.com/media/30pELz7CcpHsRINk7S/giphy.gif" />
</p>
<br><br>

[![Build Status](https://travis-ci.org/FormidableLabs/react-native-app-auth.svg?branch=master)](https://travis-ci.org/FormidableLabs/react-native-app-auth)
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
}
}

Expand All @@ -14,7 +14,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
Expand Down
37 changes: 26 additions & 11 deletions android/src/main/java/com/reactlibrary/RNAppAuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public RNAppAuthModule(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
reactContext.addActivityEventListener(this);
}
@Override
public void onNewIntent(Intent intent) {

}

@ReactMethod
public void authorize(
Expand Down Expand Up @@ -210,7 +214,18 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
AuthorizationException exception = AuthorizationException.fromIntent(data);
if (exception != null) {
promise.reject("RNAppAuth Error", "Failed to authenticate", exception);
this.promise.reject("RNAppAuth Error", "Failed to authenticate", exception);
return;
}

if (this.additionalParametersMap.containsKey("skipTokenExchange")
&& this.additionalParametersMap.get("skipTokenExchange") != null
&& this.additionalParametersMap.get("skipTokenExchange").equals("true")) {
WritableMap map = Arguments.createMap();
map.putString("code", response.authorizationCode);
map.putString("state", response.state);
map.putString("redirectUri", response.request.redirectUri.toString());
this.promise.resolve(map);
return;
}

Expand All @@ -223,8 +238,8 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,

TokenRequest tokenRequest = response.createTokenExchangeRequest(this.additionalParametersMap);

AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {

AuthorizationService.TokenResponseCallback tokenResponseCallback
= new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
TokenResponse resp, AuthorizationException ex) {
Expand Down Expand Up @@ -466,12 +481,17 @@ private AuthorizationServiceConfiguration createAuthorizationServiceConfiguratio
throw new Exception("serviceConfiguration passed without an authorizationEndpoint");
}

if (!serviceConfiguration.hasKey("tokenEndpoint")) {
Uri tokenEndpoint = Uri.EMPTY;
if (serviceConfiguration.hasKey("tokenEndpoint")) {
tokenEndpoint = Uri.parse(serviceConfiguration.getString("tokenEndpoint"));
} else if (!this.additionalParametersMap.containsKey("skipTokenExchange")
|| !this.additionalParametersMap.get("skipTokenExchange").equals("true")) {
// tokenEndpoint is required unless `skipTokenExchange` is set to true
throw new Exception("serviceConfiguration passed without a tokenEndpoint");
}

Uri authorizationEndpoint = Uri.parse(serviceConfiguration.getString("authorizationEndpoint"));
Uri tokenEndpoint = Uri.parse(serviceConfiguration.getString("tokenEndpoint"));

Uri registrationEndpoint = null;
if (serviceConfiguration.hasKey("registrationEndpoint")) {
registrationEndpoint = Uri.parse(serviceConfiguration.getString("registrationEndPoint"));
Expand All @@ -484,14 +504,9 @@ private AuthorizationServiceConfiguration createAuthorizationServiceConfiguratio
);
}


@Override
public void onNewIntent(Intent intent) {

}

@Override
public String getName() {
return "RNAppAuth";
}

}
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface BuiltInParameters {
display?: "page" | "popup" | "touch" | "wap";
login_prompt?: string;
prompt?: "consent" |"login" | "none" | "select_account";
skipTokenExchange: boolean;
}

export type AuthConfiguration = BaseAuthConfiguration & {
Expand All @@ -40,6 +41,12 @@ export interface AuthorizeResult {
tokenType: string;
}

export interface AuthorizeWithoutTokenExchangeResult {
code: string; // i.e. authorizationCode
state: string;
redirectUri: string;
}

export interface RevokeConfiguration {
tokenToRevoke: string;
sendClientId?: boolean;
Expand All @@ -49,7 +56,7 @@ export interface RefreshConfiguration {
refreshToken: string;
}

export function authorize(config: AuthConfiguration): Promise<AuthorizeResult>;
export function authorize(config: AuthConfiguration): Promise<AuthorizeResult|AuthorizeWithoutTokenExchangeResult>;

export function refresh(
config: AuthConfiguration,
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const validateIssuerOrServiceConfigurationEndpoints = (issuer, serviceConfigurat
invariant(
typeof issuer === 'string' ||
(serviceConfiguration &&
typeof serviceConfiguration.authorizationEndpoint === 'string' &&
typeof serviceConfiguration.tokenEndpoint === 'string'),
typeof serviceConfiguration.authorizationEndpoint === 'string'),
'Config error: you must provide either an issuer or a service endpoints'
);
const validateIssuerOrServiceConfigurationRevocationEndpoint = (issuer, serviceConfiguration) =>
Expand Down
44 changes: 32 additions & 12 deletions ios/RNAppAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,36 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
// performs authentication request
id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>)[UIApplication sharedApplication].delegate;

id<OIDAuthorizationFlowSession> currentSession =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
presentingViewController:appDelegate.window.rootViewController
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
if (authState) {
resolve([self formatResponse:authState.lastTokenResponse]);
} else {
reject(@"RNAppAuth Error", [error localizedDescription], error);
}

}]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
id<OIDAuthorizationFlowSession> currentSession;

if (additionalParameters[@"skipTokenExchange"] && [additionalParameters[@"skipTokenExchange"] isEqualToString:@"true"]) {
currentSession = [OIDAuthorizationService presentAuthorizationRequest:request presentingViewController:appDelegate.window.rootViewController callback:^(OIDAuthorizationResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
reject(@"RNAppAuth Auth Callback Error",[error localizedDescription], error);
} else {
NSDictionary *map = @{
@"code" : response.authorizationCode,
@"state" : response.state,
@"redirectUri" : [response.request.redirectURL absoluteString]
};
resolve(map);
}
}];
} else {
currentSession =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
presentingViewController:appDelegate.window.rootViewController
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
if (authState) {
resolve([self formatResponse:authState.lastTokenResponse]);
} else {
reject(@"RNAppAuth Error", [error localizedDescription], error);
}

}]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
}

if ([[appDelegate class] conformsToProtocol:@protocol(RNAppAuthAuthorizationFlowManager)]
&& [appDelegate respondsToSelector: @selector(setCurrentAuthorizationFlowSession:)]) {
[appDelegate setCurrentAuthorizationFlowSession:currentSession];
Expand Down Expand Up @@ -189,6 +207,8 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
codeVerifier:nil
additionalParameters:additionalParameters];



[OIDAuthorizationService performTokenRequest:tokenRefreshRequest
callback:^(OIDTokenResponse *_Nullable response,
NSError *_Nullable error) {
Expand Down