Skip to content
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ const result = await appAuth.authorize(scopes);
// returns accessToken, accessTokenExpirationDate and refreshToken
```

#### config

This is your configuration object for the client
- **issuer**: (`string`) *REQUIRED* the url of the auth server
- **clientId**: (`string`) *REQUIRED* your client id on the auth server
- **redirectUrl**: (`string`) *REQUIRED* the url that links back to your app with the auth code
- **additionalParameters**: (`object` | `null`) additional parameters that will be passed in the authorization request.
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
`hello=world&foo=bar` to the authorization request.

### `refresh`

This method will refresh the accessToken using the refreshToken. Some auth providers will also give
Expand Down Expand Up @@ -280,7 +290,7 @@ import AppAuth from 'react-native-app-auth';
// initialise the client with your configuration
const appAuth = new AppAuth({
issuer: '<YOUR_ISSUER_URL>',
clientId: '<YOUR_CLIENT_ID',
clientId: '<YOUR_CLIENT_ID>',
redirectUrl: '<YOUR_REDIRECT_URL>',
});

Expand Down
65 changes: 53 additions & 12 deletions android/src/main/java/com/reactlibrary/RNAppAuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableMap;

import net.openid.appauth.AuthorizationException;
Expand All @@ -26,6 +28,7 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class RNAppAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener {

Expand Down Expand Up @@ -63,8 +66,29 @@ private WritableMap tokenResponseToMap(TokenResponse response) {
return map;
}

private HashMap<String, String> additionalParametersToMap(ReadableMap additionalParameters) {

HashMap<String, String> additionalParametersHash = new HashMap<>();

ReadableMapKeySetIterator iterator = additionalParameters.keySetIterator();

while (iterator.hasNextKey()) {
String nextKey = iterator.nextKey();
additionalParametersHash.put(nextKey, additionalParameters.getString(nextKey));
}

return additionalParametersHash;
}

@ReactMethod
public void authorize(String issuer, final String redirectUrl, final String clientId, final ReadableArray scopes, final Promise promise) {
public void authorize(
String issuer,
final String redirectUrl,
final String clientId,
final ReadableArray scopes,
final ReadableMap additionalParameters,
final Promise promise
) {

final Context context = this.reactContext;
this.promise = promise;
Expand All @@ -88,11 +112,16 @@ public void onFetchConfigurationCompleted(
serviceConfiguration,
clientId,
ResponseTypeValues.CODE,
Uri.parse(redirectUrl));
Uri.parse(redirectUrl)
)
.setScope(scopesString);

if (additionalParameters != null) {
authRequestBuilder.setAdditionalParameters(additionalParametersToMap(additionalParameters));
}

AuthorizationRequest authRequest = authRequestBuilder.build();

AuthorizationRequest authRequest = authRequestBuilder
.setScope(scopesString)
.build();
AuthorizationService authService = new AuthorizationService(context);
Intent authIntent = authService.getAuthorizationRequestIntent(authRequest);
currentActivity.startActivityForResult(authIntent, 0);
Expand All @@ -103,7 +132,15 @@ public void onFetchConfigurationCompleted(
}

@ReactMethod
public void refresh(String issuer, final String redirectUrl, final String clientId, final String refreshToken, final ReadableArray scopes, final Promise promise) {
public void refresh(
String issuer,
final String redirectUrl,
final String clientId,
final String refreshToken,
final ReadableArray scopes,
final ReadableMap additionalParameters,
final Promise promise
) {
final Context context = this.reactContext;

final String scopesString = this.arrayToString(scopes);
Expand All @@ -123,13 +160,17 @@ public void onFetchConfigurationCompleted(
new TokenRequest.Builder(
serviceConfiguration,
clientId
);
)
.setScope(scopesString)
.setRefreshToken(refreshToken)
.setRedirectUri(Uri.parse(redirectUrl));

if (additionalParameters != null) {
tokenRequestBuilder.setAdditionalParameters(additionalParametersToMap(additionalParameters));
}

TokenRequest tokenRequest = tokenRequestBuilder.build();

TokenRequest tokenRequest = tokenRequestBuilder
.setScope(scopesString)
.setRefreshToken(refreshToken)
.setRedirectUri(Uri.parse(redirectUrl))
.build();

AuthorizationService authService = new AuthorizationService(context);

Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default class AppAuth {
this.config.issuer,
this.config.redirectUrl,
this.config.clientId,
scopes
scopes,
this.config.additionalParameters
);
}

Expand All @@ -36,7 +37,8 @@ export default class AppAuth {
this.config.redirectUrl,
this.config.clientId,
refreshToken,
scopes
scopes,
this.config.additionalParameters
);
}

Expand Down
12 changes: 10 additions & 2 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('AppAuth', () => {
issuer: 'test-issuer',
redirectUrl: 'test-redirectUrl',
clientId: 'test-clientId',
additionalParameters: { hello: 'world' },
};

beforeAll(() => {
Expand All @@ -33,6 +34,11 @@ describe('AppAuth', () => {
expect(appAuth.getConfig()).toEqual(config);
});

it('saves the additionalParameters correctly if they are empty', () => {
const appAuth = new AppAuth({ ...config, additionalParameters: undefined });
expect(appAuth.getConfig()).toEqual({ ...config, additionalParameters: undefined });
});

it('throws an error when issuer is not a string', () => {
expect(() => {
new AppAuth({ ...config, issuer: () => ({}) }); // eslint-disable-line no-new
Expand Down Expand Up @@ -81,7 +87,8 @@ describe('AppAuth', () => {
config.issuer,
config.redirectUrl,
config.clientId,
scopes
scopes,
config.additionalParameters
);
});
});
Expand Down Expand Up @@ -124,7 +131,8 @@ describe('AppAuth', () => {
config.redirectUrl,
config.clientId,
refreshToken,
scopes
scopes,
config.additionalParameters
);
});
});
Expand Down
6 changes: 4 additions & 2 deletions ios/RNAppAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ - (dispatch_queue_t)methodQueue
redirectUrl: (NSString *) redirectUrl
clientId: (NSString *) clientId
scopes: (NSArray *) scopes
additionalParameters: (NSDictionary *_Nullable) additionalParameters
resolve:(RCTPromiseResolveBlock) resolve
reject: (RCTPromiseRejectBlock) reject)
{
Expand All @@ -35,7 +36,7 @@ - (dispatch_queue_t)methodQueue
scopes:scopes
redirectURL:[NSURL URLWithString:redirectUrl]
responseType:OIDResponseTypeCode
additionalParameters:nil];
additionalParameters:additionalParameters];


// performs authentication request
Expand Down Expand Up @@ -79,6 +80,7 @@ - (dispatch_queue_t)methodQueue
clientId: (NSString *) clientId
refreshToken: (NSString *) refreshToken
scopes: (NSArray *) scopes
additionalParameters: (NSDictionary *_Nullable) additionalParameters
resolve:(RCTPromiseResolveBlock) resolve
reject: (RCTPromiseRejectBlock) reject)
{
Expand All @@ -99,7 +101,7 @@ - (dispatch_queue_t)methodQueue
scopes:scopes
refreshToken:refreshToken
codeVerifier:nil
additionalParameters:nil];
additionalParameters:additionalParameters];

[OIDAuthorizationService performTokenRequest:tokenRefreshRequest
callback:^(OIDTokenResponse *_Nullable response,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4076,7 +4076,7 @@ preserve@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"

prettier@^1.10.2:
[email protected]:
version "1.10.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.10.2.tgz#1af8356d1842276a99a5b5529c82dd9e9ad3cc93"

Expand Down