Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

🌱 Make responseType configurable #143

Merged
merged 3 commits into from
Mar 15, 2018
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
5 changes: 3 additions & 2 deletions packages/okta-angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The `OktaAuthModule` is the initializer for your OpenID Connect client configura
- `clientId` **(required)**: The OpenID Connect `client_id`
- `redirectUri` **(required)**: Where the callback is hosted
- `scope` *(optional)*: Reserved for custom claims to be returned in the tokens
- `responseType` *(optional)*: Desired token grant types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should be explicitly mentioning that these are string values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about noting that in this section, but seems weird to only mention the types on this configuration value. When this value is evaluated, typescript will error due to a type difference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aw, types 💘

- `onAuthRequired` *(optional)*: Accepts a callback to make a decision when authentication is required. If not supplied, `okta-angular` will redirect directly to Okta for authentication.

```typescript
Expand All @@ -48,8 +49,8 @@ import {

const oktaConfig = {
issuer: 'https://{yourOktaDomain}.com/oauth2/default',
redirectUri: 'http://localhost:{port}/implicit/callback',
clientId: '{clientId}'
clientId: '{clientId}',
redirectUri: 'http://localhost:{port}/implicit/callback'
}

const appRoutes: Routes = [
Expand Down
4 changes: 3 additions & 1 deletion packages/okta-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"prestart": "npm run build:dependencies",
"prepublish": "npm run ngc",
"ngc": "./node_modules/.bin/ngc -p tsconfig.json",
"test": "npm run --prefix test/e2e/harness/ e2e",
"test": "npm run lint && npm run test:unit && npm run test:e2e",
"test:unit": "npm run --prefix test/e2e/harness/ test",
"test:e2e": "npm run --prefix test/e2e/harness/ e2e",
"start": "npm run --prefix test/e2e/harness/ start",
"docs": "typedoc --options typedoc.json --exclude '{**/*.spec.ts,**/test/**}' ./src/",
"lint": "npm run --prefix test/e2e/harness/ lint",
Expand Down
1 change: 1 addition & 0 deletions packages/okta-angular/src/okta/okta.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface OktaConfig {
clientId?: string;
scope?: string;
onAuthRequired?: Function;
responseType?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should get some README love

}

export const OKTA_CONFIG = new InjectionToken<OktaConfig>('okta.config.angular');
2 changes: 1 addition & 1 deletion packages/okta-angular/src/okta/okta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class OktaAuthService {
*/
loginRedirect(additionalParams?: object) {
this.oktaAuth.token.getWithRedirect({
responseType: ['id_token', 'token'],
responseType: (this.config.responseType || 'id_token token').split(' '),
// Convert scopes to list of strings
scopes: this.config.scope.split(' '),
...additionalParams
Expand Down
2 changes: 1 addition & 1 deletion packages/okta-angular/test/e2e/harness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "npm run env && ng serve --port=3000",
"env": "node ./scripts/prebuild.js",
"build": "ng build",
"test": "ng test",
"test": "ng test -watch=false",
"lint": "ng lint",
"e2e": "npm run env && ng e2e -port 3000"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { environment } from './../environments/environment';

import {
OktaAuthGuard,
OktaAuthModule,
OktaCallbackComponent,
OktaLoginRedirectComponent
} from '@okta/okta-angular';

describe('Unit Tests', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These spec files should be siblings of the source files, it's odd that we're burying unit testing within an e2e test. We should discuss this, we need to decide if we're going to fix this now or later. I also need a place to put unit testing for the user agent work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline: this is going to be significant work and duplication of test boilerplate, so we won't do this.

let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;

beforeEach(() => {
const config = {
issuer: environment.ISSUER,
redirectUri: environment.REDIRECT_URI,
clientId: environment.CLIENT_ID,
scope: 'email',
responseType: 'id_token'
};

TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([{ path: 'foo', redirectTo: '/foo' }]),
OktaAuthModule.initAuth(config)
],
declarations: [
AppComponent
]
});
});

beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create the app', async(() => {
expect(component).toBeTruthy();
}));

it('should instantiate the OktaAuth object', async(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really should be testing that we correctly pass this down to the AuthJS constructor on the login redirect. To do so we would need to write a provider (or service?) wrapper around AuthJS so that it can easily be mocked via DI in test and observed. Out of scope for this PR, since we'll have to touch a lot of code with that change.

const config = component.oktaAuth.getOktaConfig();
expect(config.issuer).toBe(environment.ISSUER);
expect(config.redirectUri).toBe(environment.REDIRECT_URI);
expect(config.clientId).toBe(environment.CLIENT_ID);
expect(config.scope).toBe('email openid');
expect(config.responseType).toBe('id_token');
}));
});
11 changes: 6 additions & 5 deletions packages/okta-angular/test/e2e/harness/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import {
*/
import { ProtectedComponent } from './protected.component';
import { AppComponent } from './app.component';
import { SessionTokenLogin } from './sessionToken-login.component';
import { SessionTokenLoginComponent } from './sessionToken-login.component';

export function onNeedsAuthenticationGuard({ oktaAuth, router }) {
router.navigate(['/sessionToken-login']);
};
}

export function onNeedsGlobalAuthenticationGuard({ oktaAuth, router }) {
router.navigate(['/login']);
};
}

const appRoutes: Routes = [
{
Expand All @@ -48,7 +48,7 @@ const appRoutes: Routes = [
},
{
path: 'sessionToken-login',
component: SessionTokenLogin
component: SessionTokenLoginComponent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used only in our test suite. We aren't exposing any of this via our SDK, so we aren't introducing a breaking change.

},
{
path: 'implicit/callback',
Expand All @@ -74,6 +74,7 @@ const config = {
redirectUri: environment.REDIRECT_URI,
clientId: environment.CLIENT_ID,
scope: 'email',
responseType: 'id_token token',
onAuthRequired: onNeedsGlobalAuthenticationGuard
};

Expand All @@ -86,7 +87,7 @@ const config = {
declarations: [
AppComponent,
ProtectedComponent,
SessionTokenLogin
SessionTokenLoginComponent
],
bootstrap: [ AppComponent ]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { OktaAuthService } from '@okta/okta-angular';
import OktaAuth from '@okta/okta-auth-js';

@Component({
selector: 'app-sessionLogin',
selector: 'app-session-login',
template: `
<router-outlet></router-outlet>

<div>
<br/>
<label>
Expand All @@ -32,7 +32,7 @@ import OktaAuth from '@okta/okta-auth-js';
</div>
`
})
export class SessionTokenLogin {
export class SessionTokenLoginComponent {
oktaAuth: OktaAuth;

constructor(private okta: OktaAuthService) {
Expand Down