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

chore(typescript): Add better TypeScript definitions for Angular #116

Closed
wants to merge 5 commits into from
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
4 changes: 2 additions & 2 deletions packages/okta-angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

export { OktaAuthModule } from './okta/okta.module';
export { OktaAuthGuard } from './okta/okta.guard';
export { OktaAuthService } from './okta/okta.service';
export { OKTA_CONFIG } from './okta/okta.config';
export { OktaAuthService } from './okta/services/okta.service';
export { OKTA_CONFIG } from './okta/models/okta.config';

// Okta View Components
export { OktaCallbackComponent, OktaLoginRedirectComponent } from './okta/components';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { Component } from '@angular/core';

import { OktaAuthService } from '../okta.service';
import { OktaAuthService } from '../services/okta.service';

@Component({template: `` })
export class OktaCallbackComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { Component } from '@angular/core';

import { OktaAuthService } from '../okta.service';
import { OktaAuthService } from '../services/okta.service';

@Component({ template: `` })
export class OktaLoginRedirectComponent {
Expand Down
17 changes: 17 additions & 0 deletions packages/okta-angular/src/okta/models/auth-required-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2017, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

import { Router } from '@angular/router';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add the license banner to this file? An example is available here.

Copy link
Author

Choose a reason for hiding this comment

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

Done


import { OktaAuthService } from '../services/okta.service';

export type AuthRequiredFunction = (oktaAuth: OktaAuthService, router: Router) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

import { InjectionToken } from '@angular/core';

import { AuthRequiredFunction } from './auth-required-function';

export interface OktaConfig {
issuer?: string;
redirectUri?: string;
clientId?: string;
scope?: string;
onAuthRequired?: Function;
onAuthRequired?: AuthRequiredFunction;
}

export const OKTA_CONFIG = new InjectionToken<OktaConfig>('okta.config.angular');
17 changes: 7 additions & 10 deletions packages/okta-angular/src/okta/okta.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ import {
Router
} from '@angular/router';

import { OktaAuthService } from './okta.service';
import { OktaAuthService } from './services/okta.service';
import { AuthRequiredFunction } from './models/auth-required-function';

@Injectable()
export class OktaAuthGuard implements CanActivate {
private oktaAuth: OktaAuthService;

constructor(private okta: OktaAuthService, private router: Router) {
this.oktaAuth = okta;
}
constructor(private oktaAuth: OktaAuthService, private router: Router) { }

/**
* Gateway for protected route. Returns true if there is a valid accessToken,
* otherwise it will cache the route and start the login flow.
* @param route
* @param state
* @param route
* @param state
*/
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.oktaAuth.isAuthenticated()) {
Expand All @@ -43,13 +40,13 @@ export class OktaAuthGuard implements CanActivate {
* Get the operation to perform on failed authentication from
* either the global config or route data injection.
*/
const onAuthRequired = route.data['onAuthRequired'] || this.oktaAuth.getOktaConfig().onAuthRequired;
const onAuthRequired: AuthRequiredFunction = route.data['onAuthRequired'] || this.oktaAuth.getOktaConfig().onAuthRequired;

if (onAuthRequired){
onAuthRequired(this.oktaAuth, this.router);
}

/**
/**
* Store the current path
*/
this.oktaAuth.setFromUri(state.url);
Expand Down
4 changes: 2 additions & 2 deletions packages/okta-angular/src/okta/okta.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import { NgModule, InjectionToken } from '@angular/core';

import { OktaCallbackComponent, OktaLoginRedirectComponent } from './components/';
import { OktaAuthService } from './okta.service';
import { OktaAuthService } from './services/okta.service';
import { OktaAuthGuard } from './okta.guard';
import { OktaConfig, OKTA_CONFIG } from './okta.config';
import { OktaConfig, OKTA_CONFIG } from './models/okta.config';

@NgModule({
declarations: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import { Inject, Injectable, Optional } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { Router } from '@angular/router';

import { OKTA_CONFIG } from './okta.config';
import { OKTA_CONFIG, OktaConfig } from '../models/okta.config';

/**
* Import the okta-auth-js library
Expand All @@ -22,11 +22,11 @@ import * as OktaAuth from '@okta/okta-auth-js';

@Injectable()
export class OktaAuthService {
private oktaAuth;
private config;
private oktaAuth: OktaAuth;
private config: OktaConfig;

constructor(@Inject(OKTA_CONFIG) private auth, private router: Router) {
const missing: any[] = [];
constructor(@Inject(OKTA_CONFIG) private auth: OktaConfig, private router: Router) {
const missing: string[] = [];

if (!auth.issuer) {
missing.push('issuer');
Expand Down Expand Up @@ -63,35 +63,37 @@ export class OktaAuthService {
/**
* Returns the OktaAuth object to handle flows outside of this lib.
*/
getOktaAuth() {
getOktaAuth(): OktaAuth {
return this.oktaAuth;
}

/**
* Checks if there is a current accessToken in the TokenManager.
*/
isAuthenticated() {
isAuthenticated(): boolean {
return !!this.oktaAuth.tokenManager.get('accessToken');
}

Copy link
Author

Choose a reason for hiding this comment

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

Not sure what the type definitions of getAccessToken and getIdToken should be. Does @okta/okta-auth-js contain a structure for it?

Copy link
Author

Choose a reason for hiding this comment

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

Looks like #115 corrects it.

/**
* Returns the current accessToken in the tokenManager.
*/
getAccessToken() {
return this.oktaAuth.tokenManager.get('accessToken');
async getAccessToken(): Promise<string | undefined> {
const accessToken = this.oktaAuth.tokenManager.get('accessToken');
return accessToken ? accessToken.accessToken : undefined;
}

/**
* Returns the current idToken in the tokenManager.
*/
getIdToken() {
return this.oktaAuth.tokenManager.get('idToken');
async getIdToken(): Promise<string | undefined> {
const idToken = this.oktaAuth.tokenManager.get('idToken');
return idToken ? idToken.idToken : undefined;
}

/**
* Returns the configuration object used.
*/
getOktaConfig(){
getOktaConfig(): OktaConfig {
return this.config;
}

Expand All @@ -109,16 +111,16 @@ export class OktaAuthService {

/**
* Stores the intended path to redirect after successful login.
* @param uri
* @param uri
*/
setFromUri(uri) {
setFromUri(uri: string) {
localStorage.setItem('referrerPath', uri);
}

/**
* Returns the referrer path from localStorage or app root.
*/
getFromUri() {
getFromUri(): string {
const path = localStorage.getItem('referrerPath') || '/';
localStorage.removeItem('referrerPath');
return path;
Expand All @@ -127,7 +129,7 @@ export class OktaAuthService {
/**
* Parses the tokens from the callback URL.
*/
async handleAuthentication() {
async handleAuthentication(): Promise<void> {
const tokens = await this.oktaAuth.token.parseFromUrl();
tokens.forEach(token => {
if (token.idToken) {
Expand All @@ -148,20 +150,18 @@ export class OktaAuthService {
* Clears the user session in Okta and removes
* tokens stored in the tokenManager.
*/
async logout() {
async logout(): Promise<void> {
this.oktaAuth.tokenManager.clear();
await this.oktaAuth.signOut();
}

/**
* Scrub scopes to ensure 'openid' is included
* @param scopes
*/
scrubScopes(scopes) {
scrubScopes(scopes: string): string {
if (!scopes) {
return 'openid email';
} else {
// Make sure object is a string
scopes = Array.isArray(scopes) ? scopes.join(' ') : scopes
}
if (scopes.indexOf('openid') === -1) {
return scopes + ' openid';
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 @@ -22,7 +22,7 @@
"@angular/platform-browser": "^4.2.4",
"@angular/platform-browser-dynamic": "^4.2.4",
"@angular/router": "^4.2.4",
"@okta/okta-angular": "file:../../../okta-okta-angular-0.0.8.tgz",
"@okta/okta-angular": "file:../../../okta-okta-angular-0.0.13.tgz",
"core-js": "^2.4.1",
"ejs": "^2.5.6",
"rxjs": "^5.4.2",
Expand Down
4 changes: 1 addition & 3 deletions packages/okta-angular/test/e2e/harness/scripts/prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

#!/usr/bin/env node

/** Angular CLI does not support environment variables the same
* way Node apps do. See:
* https://github.com/angular/angular-cli/issues/4318
Expand Down Expand Up @@ -47,4 +45,4 @@ const output = ejs.render(environmentTemplate, Object.assign({}, defaultEnvValue
// Write environment file
fs.writeFileSync(path.join(environmentFilesDirectory, targetEnvironmentFileName), output);

process.exit(0);
process.exit(0);
7 changes: 4 additions & 3 deletions packages/okta-angular/test/e2e/harness/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Routes, RouterModule, Router } from '@angular/router';

import { environment } from './../environments/environment';

Expand All @@ -22,6 +22,7 @@ import { environment } from './../environments/environment';
import {
OktaAuthGuard,
OktaAuthModule,
OktaAuthService,
OktaCallbackComponent,
OktaLoginRedirectComponent
} from '@okta/okta-angular';
Expand All @@ -33,11 +34,11 @@ import { ProtectedComponent } from './protected.component';
import { AppComponent } from './app.component';
import { SessionTokenLogin } from './sessionToken-login.component';

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

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

Expand Down