Skip to content

Commit 7530d4b

Browse files
committed
feat(auth0): addition of auth0 plugin from community
1 parent 3230c03 commit 7530d4b

File tree

104 files changed

+8319
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+8319
-13
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
# @nativescript/\* plugins
2-
3-
```
4-
npm run setup
5-
npm start
6-
```
7-
81
- [@nativescript/animated-circle](packages/animated-circle/README.md)
92
- [@nativescript/appavailability](packages/appavailability/README.md)
103
- [@nativescript/apple-sign-in](packages/apple-sign-in/README.md)
4+
- [@nativescript/auth0](packages/auth0/README.md)
115
- [@nativescript/auto-fit-text](packages/auto-fit-text/README.md)
126
- [@nativescript/background-http](packages/background-http/README.md)
137
- [@nativescript/biometrics](packages/biometrics/README.md)

apps/demo-angular/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"main": "./src/main.ts",
33
"dependencies": {
44
"@nativescript/core": "file:../../node_modules/@nativescript/core",
5-
"@nativescript/pdf": "file:../../dist/packages/pdf",
5+
"@nativescript/auth0": "file:../../dist/packages/auth0",
66
"@nativescript/animated-circle": "file:../../dist/packages/animated-circle",
77
"@nativescript/appavailability": "file:../../dist/packages/appavailability",
88
"@nativescript/apple-sign-in": "file:../../dist/packages/apple-sign-in",
@@ -30,6 +30,7 @@
3030
"@nativescript/iqkeyboardmanager": "file:../../dist/packages/iqkeyboardmanager",
3131
"@nativescript/local-notifications": "file:../../dist/packages/local-notifications",
3232
"@nativescript/localize": "file:../../dist/packages/localize",
33+
"@nativescript/pdf": "file:../../dist/packages/pdf",
3334
"@nativescript/picker": "file:../../dist/packages/picker",
3435
"@nativescript/shared-notification-delegate": "file:../../dist/packages/shared-notification-delegate",
3536
"@nativescript/social-share": "file:../../dist/packages/social-share",

apps/demo-angular/src/app-routing.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const routes: Routes = [
1010
{ path: 'animated-circle', loadChildren: () => import('./plugin-demos/animated-circle.module').then((m) => m.AnimatedCircleModule) },
1111
{ path: 'appavailability', loadChildren: () => import('./plugin-demos/appavailability.module').then((m) => m.AppavailabilityModule) },
1212
{ path: 'apple-sign-in', loadChildren: () => import('./plugin-demos/apple-sign-in.module').then((m) => m.AppleSignInModule) },
13+
{ path: 'auth0', loadChildren: () => import('./plugin-demos/auth0.module').then((m) => m.Auth0Module) },
1314
{ path: 'auto-fit-text', loadChildren: () => import('./plugin-demos/auto-fit-text.module').then((m) => m.AutoFitTextModule) },
1415
{ path: 'background-http', loadChildren: () => import('./plugin-demos/background-http.module').then((m) => m.BackgroundHttpModule) },
1516
{ path: 'biometrics', loadChildren: () => import('./plugin-demos/biometrics.module').then((m) => m.BiometricsModule) },

apps/demo-angular/src/home.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class HomeComponent {
1515
{
1616
name: 'apple-sign-in',
1717
},
18+
{
19+
name: 'auth0',
20+
},
1821
{
1922
name: 'auto-fit-text',
2023
},
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<ActionBar title="auth0" class="action-bar"> </ActionBar>
2+
<StackLayout class="p-20">
3+
<ScrollView class="h-full">
4+
<StackLayout>
5+
<Button text="Test auth0" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
6+
</StackLayout>
7+
</ScrollView>
8+
</StackLayout>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, NgZone } from '@angular/core';
2+
import { DemoSharedAuth0 } from '@demo/shared';
3+
import {} from '@nativescript/auth0';
4+
5+
@Component({
6+
selector: 'demo-auth0',
7+
templateUrl: 'auth0.component.html',
8+
})
9+
export class Auth0Component {
10+
demoShared: DemoSharedAuth0;
11+
12+
constructor(private _ngZone: NgZone) {}
13+
14+
ngOnInit() {
15+
this.demoShared = new DemoSharedAuth0();
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { NativeScriptCommonModule, NativeScriptRouterModule } from '@nativescript/angular';
3+
import { Auth0Component } from './auth0.component';
4+
5+
@NgModule({
6+
imports: [NativeScriptCommonModule, NativeScriptRouterModule.forChild([{ path: '', component: Auth0Component }])],
7+
declarations: [Auth0Component],
8+
schemas: [NO_ERRORS_SCHEMA],
9+
})
10+
export class Auth0Module {}

apps/demo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": "<fill-your-repository-here>",
66
"dependencies": {
77
"@nativescript/core": "file:../../node_modules/@nativescript/core",
8-
"@nativescript/pdf": "file:../../packages/pdf",
8+
"@nativescript/auth0": "file:../../packages/auth0",
99
"@nativescript/animated-circle": "file:../../packages/animated-circle",
1010
"@nativescript/appavailability": "file:../../packages/appavailability",
1111
"@nativescript/apple-sign-in": "file:../../packages/apple-sign-in",
@@ -33,6 +33,7 @@
3333
"@nativescript/iqkeyboardmanager": "file:../../packages/iqkeyboardmanager",
3434
"@nativescript/local-notifications": "file:../../packages/local-notifications",
3535
"@nativescript/localize": "file:../../packages/localize",
36+
"@nativescript/pdf": "file:../../packages/pdf",
3637
"@nativescript/picker": "file:../../packages/picker",
3738
"@nativescript/shared-notification-delegate": "file:../../packages/shared-notification-delegate",
3839
"@nativescript/social-share": "file:../../packages/social-share",

apps/demo/src/main-page.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<Button text="animated-circle" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
99
<Button text="appavailability" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
1010
<Button text="apple-sign-in" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
11+
<Button text="auth0" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
1112
<Button text="auto-fit-text" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
1213
<Button text="background-http" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
1314
<Button text="biometrics" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>

apps/demo/src/plugin-demos/auth0.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Observable, EventData, Page } from '@nativescript/core';
2+
import { DemoSharedAuth0 } from '@demo/shared';
3+
import {} from '@nativescript/auth0';
4+
5+
export function navigatingTo(args: EventData) {
6+
const page = <Page>args.object;
7+
page.bindingContext = new DemoModel();
8+
}
9+
10+
export class DemoModel extends DemoSharedAuth0 {}

apps/demo/src/plugin-demos/auth0.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
2+
<Page.actionBar>
3+
<ActionBar title="auth0" icon="" class="action-bar">
4+
</ActionBar>
5+
</Page.actionBar>
6+
<StackLayout class="p-20">
7+
<ScrollView class="h-full">
8+
<StackLayout>
9+
<Button text="Test auth0" tap="{{ testIt }}" class="btn btn-primary"/>
10+
11+
</StackLayout>
12+
</ScrollView>
13+
</StackLayout>
14+
</Page>

packages/auth0/.eslintrc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*", "node_modules/**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
}
17+
]
18+
}

packages/auth0/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @nativescript/auth0
2+
3+
```javascript
4+
ns plugin add @nativescript/auth0
5+
```
6+
7+
## Usage
8+
9+
// TODO
10+
11+
## License
12+
13+
Apache License Version 2.0

packages/auth0/android/auth0.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import Context = android.content.Context;
2+
import Uri = android.net.Uri;
3+
4+
import { Telemetry } from './util/telemetry';
5+
6+
/**
7+
* Represents your Auth0 account information (clientId {@literal &} domain),
8+
* and it's used to obtain clients for Auth0's APIs.
9+
* <pre>{@code
10+
* Auth0 auth0 = new Auth0("YOUR_CLIENT_ID", "YOUR_DOMAIN");
11+
* }</pre>
12+
* For more information, please see the <a href="https://auth0.com/docs/api-auth/tutorials/adoption">OIDC adoption guide</a>.
13+
*
14+
* @see Auth0#setOIDCConformant(boolean)
15+
*/
16+
export class Auth0 {
17+
private static readonly AUTH0_US_CDN_URL: string = 'https://cdn.auth0.com';
18+
private static readonly DOT_AUTH0_DOT_COM: string = '.auth0.com';
19+
20+
private readonly clientId: string;
21+
private readonly domainUrl: Uri;
22+
private readonly configurationUrl: Uri;
23+
private telemetry: Telemetry;
24+
private loggingEnabled: boolean;
25+
private tls12Enforced: boolean;
26+
27+
/**
28+
* Creates a new object using clientId, domain and configuration domain.
29+
*
30+
* @param clientId of your Auth0 application
31+
* @param domain of your Auth0 account
32+
* @param configurationDomain where Auth0's configuration will be fetched. By default is Auth0 public cloud
33+
*/
34+
constructor(clientId: string, domain: string, configurationDomain?: string) {
35+
this.clientId = clientId;
36+
this.domainUrl = this.ensureValidUrl(domain);
37+
if (this.domainUrl == null) {
38+
throw new java.lang.IllegalArgumentException(`Invalid domain url: '${domain}'`);
39+
}
40+
this.configurationUrl = this.resolveConfiguration(configurationDomain, this.domainUrl);
41+
this.telemetry = new Telemetry('Auth0.NativeScript', '0.0.0');
42+
}
43+
44+
/**
45+
* @return your Auth0 application client identifier
46+
*/
47+
public getClientId(): string {
48+
return this.clientId;
49+
}
50+
51+
/**
52+
* @return your Auth0 account domain url
53+
*/
54+
public getDomainUrl(): string {
55+
return this.domainUrl.toString();
56+
}
57+
58+
/**
59+
* @return your account configuration url
60+
*/
61+
public getConfigurationUrl(): string {
62+
return this.configurationUrl.toString();
63+
}
64+
65+
/**
66+
* @return Url to perform the web flow of OAuth
67+
*/
68+
public getAuthorizeUrl(): string {
69+
return this.domainUrl.buildUpon().appendEncodedPath('authorize').build().toString();
70+
}
71+
72+
/**
73+
* @return Auth0 telemetry info sent in every request
74+
*/
75+
public getTelemetry(): Telemetry {
76+
return this.telemetry;
77+
}
78+
79+
/**
80+
* Setter for the Telemetry to send in every request to Auth0.
81+
*
82+
* @param telemetry to send in every request to Auth0
83+
*/
84+
public setTelemetry(telemetry: Telemetry): void {
85+
this.telemetry = telemetry;
86+
}
87+
88+
/**
89+
* Avoid sending telemetry in every request to Auth0
90+
*/
91+
public doNotSendTelemetry(): void {
92+
this.telemetry = null;
93+
}
94+
95+
/**
96+
* Getter for the HTTP logger is enabled or not.
97+
*
98+
* @return whether every Request, Response and other sensitive information should be logged or not.
99+
*/
100+
public isLoggingEnabled(): boolean {
101+
return this.loggingEnabled;
102+
}
103+
104+
/**
105+
* Log every Request, Response and other sensitive information exchanged using the Auth0 APIs.
106+
* You shouldn't enable logging in release builds as it may leak sensitive information.
107+
*
108+
* @param enabled if every Request, Response and other sensitive information should be logged.
109+
*/
110+
public setLoggingEnabled(enabled: boolean): void {
111+
this.loggingEnabled = enabled;
112+
}
113+
114+
/**
115+
* Getter for whether TLS 1.2 is enforced on devices with API 16-21.
116+
*
117+
* @return whether TLS 1.2 is enforced on devices with API 16-21.
118+
*/
119+
public isTLS12Enforced(): boolean {
120+
return this.tls12Enforced;
121+
}
122+
123+
/**
124+
* Set whether to enforce TLS 1.2 on devices with API 16-21.
125+
*
126+
* @param enforced whether TLS 1.2 is enforced on devices with API 16-21.
127+
*/
128+
public setTLS12Enforced(enforced: boolean): void {
129+
this.tls12Enforced = enforced;
130+
}
131+
132+
private resolveConfiguration(configurationDomain: string, domainUrl: Uri): Uri {
133+
let url = this.ensureValidUrl(configurationDomain);
134+
if (url == null) {
135+
const host = domainUrl.getHost();
136+
if (host.endsWith(Auth0.DOT_AUTH0_DOT_COM)) {
137+
const parts = host.split('.');
138+
if (parts.length > 3) {
139+
url = Uri.parse(`https://cdn.${parts[parts.length - 3]}${Auth0.DOT_AUTH0_DOT_COM}`);
140+
} else {
141+
url = Uri.parse(Auth0.AUTH0_US_CDN_URL);
142+
}
143+
} else {
144+
url = domainUrl;
145+
}
146+
}
147+
return url;
148+
}
149+
150+
private ensureValidUrl(url: string): Uri {
151+
if (url == null) {
152+
return null;
153+
}
154+
const safeUrl = url.startsWith('http') ? url : 'https://' + url;
155+
return Uri.parse(safeUrl);
156+
}
157+
158+
private static getResourceFromContext(context: Context, resName: string): string {
159+
const stringRes: number = context.getResources().getIdentifier(resName, 'string', context.getPackageName());
160+
if (stringRes === 0) {
161+
throw new java.lang.IllegalArgumentException(`The 'R.string.${resName}' value it's not defined in your project's resources file.`);
162+
}
163+
return context.getString(stringRes);
164+
}
165+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Base Exception for any error found during a request to Auth0's API
3+
*/
4+
export class Auth0Exception extends Error {
5+
constructor(message?: string) {
6+
super(message);
7+
Object.setPrototypeOf(this, new.target.prototype);
8+
}
9+
10+
public static readonly UNKNOWN_ERROR: string = 'a0.sdk.internal_error.unknown';
11+
public static readonly NON_JSON_ERROR: string = 'a0.sdk.internal_error.plain';
12+
public static readonly EMPTY_BODY_ERROR: string = 'a0.sdk.internal_error.empty';
13+
public static readonly EMPTY_RESPONSE_BODY_DESCRIPTION: string = 'Empty response body';
14+
}

0 commit comments

Comments
 (0)