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: use server bundle for react native #363

Merged
merged 5 commits into from
Apr 20, 2020
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 3.1.0

### Features

- [#363](https://github.com/okta/okta-auth-js/pull/363)
- Expose server bundle for React Native platform as an Authentication SDK.
- Handle userAgent customization with newly added userAgent field in config.

## 3.0.1

### Bug Fixes
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ In most cases you will not need to set a value for `responseMode`. Defaults are
* `secure`: Defaults to `true`, unless the application origin is `http://localhost`, in which case it is forced to `false`. If `true`, the SDK will set the "Secure" option on all cookies. When this option is `true`, an exception will be thrown if the application origin is not using the HTTPS protocol. Setting to `false` will allow setting cookies on an HTTP origin, but is not recommended for production applications.
* `sameSite`: Defaults to `none` if the `secure` option is `true`, or `lax` if the `secure` option is false. Allows fine-grained control over the same-site cookie setting. A value of `none` allows embedding within an iframe. A value of `lax` will avoid being blocked by user "3rd party" cookie settings. A value of `strict` will block all cookies when redirecting from Okta and is not recommended.

| `userAgent` | An object to customize SDK information in `User-Agent` or `X-Okta-User-Agent-Extended` http header. Okta's downstream SDKs of `okta-auth-js` should provide customized userAgent for analytics purpose. |

* `value`: Exact value to use as customized userAgent.
* `template`: String template with `$OKTA_AUTH_JS` placeholder. `okta-auth-js` will replace the placeholder with current module information.

##### Example Client

```javascript
Expand Down Expand Up @@ -1906,22 +1911,22 @@ authClient.tokenManager.off('renewed');
authClient.tokenManager.off('renewed', myRenewedCallback);
```

## Node JS Usage
## Node JS and React Native Usage

You can use this library on server side in your Node application as an Authentication SDK. It can only be used in this way for communicating with the [Authentication API](https://developer.okta.com/docs/api/resources/authn), **not** to implement an OIDC flow.
You can use this library on server side in your Node application or mobile client side in React Native environment as an Authentication SDK. It can only be used in this way for communicating with the [Authentication API](https://developer.okta.com/docs/api/resources/authn), **not** to implement an OIDC flow.

To include this library in your project, you can follow the instructions in the [Getting started](#getting-started) section.

### Configuration

You only need to set the `url` for your Okta Domain:
You only need to set the `issuer` for your Okta Domain:

```javascript
var OktaAuth = require('@okta/okta-auth-js');

var config = {
// The URL for your Okta organization
url: 'https://{yourOktaDomain}'
issuer: 'https://{yourOktaDomain}'
};

var authClient = new OktaAuth(config);
Expand Down
2 changes: 1 addition & 1 deletion packages/okta-auth-js/lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function OktaAuthBuilder(args) {
throw new AuthSdkError(errorMessage);
}

this.userAgent = 'okta-auth-js-' + SDK_VERSION;
this.userAgent = builderUtil.getUserAgent(args, SDK_VERSION) || 'okta-auth-js-' + SDK_VERSION;

// Digital clocks will drift over time, so the server
// can misalign with the time reported by the browser.
Expand Down
30 changes: 29 additions & 1 deletion packages/okta-auth-js/lib/builderUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var tx = require('./tx');
var util = require('./util');

// TODO: use @okta/configuration-validation (move module to this monorepo?)
// eslint-disable-next-line complexity
function assertValidConfig(args) {
if (!args) {
throw new AuthSdkError('No arguments passed to constructor. ' +
Expand All @@ -37,6 +38,14 @@ function assertValidConfig(args) {
throw new AuthSdkError('Issuer URL passed to constructor contains "-admin" in subdomain. ' +
'Required usage: new OktaAuth({issuer: "https://{yourOktaDomain}.com})');
}

var userAgent = args.userAgent;
var userAgentTemplateWithNoPlaceholder =
userAgent && userAgent.template && userAgent.template.indexOf('$OKTA_AUTH_JS') === -1;
if (userAgentTemplateWithNoPlaceholder) {
throw new AuthSdkError('UserAgentTemplate must include "$OKTA_AUTH_JS" placeholder. ' +
'Required usage: new OktaAuth({userAgentTemplate: "xxx $OKTA_AUTH_JS xxx"})');
}
}

function addSharedPrototypes(proto) {
Expand Down Expand Up @@ -91,8 +100,27 @@ function buildOktaAuth(OktaAuthBuilder) {
};
}

function getUserAgent(args, sdkVersion) {
var userAgent = args.userAgent;

if (!userAgent) {
return '';
}

if (userAgent.value) {
return userAgent.value;
}

if (userAgent.template) {
return userAgent.template.replace('$OKTA_AUTH_JS', `okta-auth-js/${sdkVersion}`);
}

return '';
}

module.exports = {
addSharedPrototypes: addSharedPrototypes,
buildOktaAuth: buildOktaAuth,
assertValidConfig: assertValidConfig
assertValidConfig: assertValidConfig,
getUserAgent: getUserAgent
};
2 changes: 1 addition & 1 deletion packages/okta-auth-js/lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function OktaAuthBuilder(args) {
headers: args.headers
};

this.userAgent = 'okta-auth-js-server' + SDK_VERSION;
this.userAgent = builderUtil.getUserAgent(args, SDK_VERSION) || 'okta-auth-js-server' + SDK_VERSION;

sdk.tx = {
status: util.bind(tx.transactionStatus, null, sdk),
Expand Down
3 changes: 2 additions & 1 deletion packages/okta-auth-js/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@okta/okta-auth-js",
"description": "The Okta Auth SDK",
"version": "3.0.1",
"version": "3.1.0",
"homepage": "https://github.com/okta/okta-auth-js",
"license": "Apache-2.0",
"main": "lib/server/serverIndex.js",
"browser": "dist/okta-auth-js.min.js",
"react-native": "lib/server/serverIndex.js",
"repository": {
"type": "git",
"url": "https://github.com/okta/okta-auth-js.git"
Expand Down
33 changes: 33 additions & 0 deletions packages/okta-auth-js/test/spec/builderUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const builderUtil = require('../../lib/builderUtil');

const SDK_VERSION = '0.0.0';

describe('builderUtil', () => {

describe('getUserAgent', () => {
it('should return userAgent if "userAgent" is provided in args', () => {
const args = {
userAgent: {
value: 'fake userAgent'
}
};
const userAgent = builderUtil.getUserAgent(args);
expect(userAgent).toEqual('fake userAgent');
});
it('should replace "$OKTA_AUTH_JS" with current authJs version if only with userAgentTemplate in args', () => {
const args = {
userAgent: {
template: 'fake userAgent $OKTA_AUTH_JS'
}
};
const userAgent = builderUtil.getUserAgent(args, SDK_VERSION);
expect(userAgent).toEqual(`fake userAgent okta-auth-js/0.0.0`);
});
it('should return undefined if neither with userAgent nor userAgentTemplate in args', () => {
const args = {};
const userAgent = builderUtil.getUserAgent(args);
expect(userAgent).toEqual('');
});
});

});
2 changes: 1 addition & 1 deletion test/app/src/testApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Object.assign(TestApp.prototype, {
${ tokensHTML({idToken, accessToken})}
`;
}

// Unauthenticated user, Login page
return `
<strong>Greetings, unknown user!</strong>
Expand Down