Giter Site home page Giter Site logo

judedaryl / nativescript-azure-auth Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 4.0 2.11 MB

ADAL wrapper for nativescript applications with logout feature. Only supports single user login.

License: Apache License 2.0

Shell 8.78% TypeScript 85.38% Ruby 0.32% JavaScript 5.52%

nativescript-azure-auth's Introduction

Azure Auth

A nativescript wrapper around the native libraries of Microsoft Azure's Active Directory Authentication Libraries ADAL for iOS and ADAl for Android.

NPM

Installation

tns plugin add nativescript-azure-auth

Usage - Angular

Create a authentication service that uses this library.

import { Injectable } from "@angular/core";
import { AzureAuth, AzureUser } from "nativescript-azure-auth";

const azureAuth: AzureAuth;
const authority: string = "https://login.microsoftonline.com/{TENANT_ID}/oauth2/authorize"
const clientID: string = "{CLIENT_ID}";
const resourceId: string = "{RESOURCE_ID}";
const redirectUri: string = "{REPLY-URL}";

@Injectable()
export class AzureAuthenticationService {
    private azureAuth: AzureAuth;
    constructor() {
        this.azureAuth = new AzureAuth(authority,clientID,resourceId,redirectUri);
    }

    login() {
        this.azureAuth.login()
            .then(accessToken => {
                console.log(`Token: ${accessToken}`);
            })
            .catch(error => {
                console.log(error);
            });
    }

    getToken() {
        this.azureAuth.getToken()
            .then(accessToken => {
                console.log(`Access token : ${accessToken}`);
            })
            .catch(error => {
                console.log(error);
            });
    }

    
    getUser() {
        this.azureAuth.getUser()
            .then((user: AzureUser) => {
                console.log(`Access token : ${JSON.stringify(user)}`);
            })
            .catch(error => {
                console.log(error);
            });
    }


    logout() {
        this.azureAuth.clearCache();
    }

}

The login and logout can be consumed in a component.

import { Component, OnInit, ViewChild, AfterViewInit, ChangeDetectorRef } from "@angular/core";
import { AzureAuthenticationService } from "~azure-authentication.service"

@Component({
  selector: "ns-app",
  templateUrl: "app.component.html"
})
export class AppComponent implements OnInit, AfterViewInit {
    
    constructor(private azureAuthService: AzureAuthenticationService) {}

    login() {
        this.azureAuthService.login();
    }

    logout() {
        this.azureAuthService.logout();
    }
}

The best use-case for getToken would be in an interceptor so you can be sure that the latest access token is attached to your header when you make a http request to a protected Rest API.

import { Component, Inject, Injectable } from "@angular/core";
import { AzureAuthenticationService } from "~azure-authentication.service"
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpResponse, HttpHeaders, HttpClient} from "@angular/common/http";
import { Observable, from } from "rxjs";
import "rxjs/add/operator/switchMap";

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
    constructor(private azureAuthService: AzureAuthenticationService) {}

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const promise = this.auth.azureAuthService.getToken();
        const observable = from(promise);
        return observable.switchMap((token) => {
            req = req.clone({setHeaders: {
                authorization: `Bearer ${token}`,
                "content-type": "application/json"
            }});
            return next
                .handle(req)
                .do(event => {})
                .catch(err => console.log(err));
        });   
    }
}

API

public login(clearCache?: boolean): Promise<string>

Opens a webview that redirects to login.microsoft.com.

clearCache parameter clears the ADAL tokenCache when set to true. By default clearCache is set to true.

public getToken(): Promise<string>

Silently retrieves a token from Azure Active Directory using the refresh token being stored by the respective native libraries for ADAL. Best used with an interceptor or when creating http request that require th token.

public clearCache(): void

Clears the entire token cache for the clientId set when instantiating this library.

Contributions

License

Apache License Version 2.0, January 2004

nativescript-azure-auth's People

Contributors

alessiobianchini avatar judedaryl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nativescript-azure-auth's Issues

Error while adding this service into constructor

Hi,

I am trying to use this plugin in my project, unfortunately this plugin gives me an error while adding the object in the constructor with private var,

ERROR NullInjectorError: StaticInjectorError(AppModule)[AppComponent -> AzureAuthenticationService]: 
JS:   StaticInjectorError(Platform: core)[AppComponent -> AzureAuthenticationService]: 
JS:     NullInjectorError: No provider for AzureAuthenticationService!

below line causing problem

constructor( private azureAuthService: AzureAuthenticationService)

Please let me know what is the reason for that.

Thank you.

Getting Error while login to microsofot

Hi,

I am getting below error after login call.

com.microsoft.aad.adal.AuthenticationCancelError: User cancelled the flow RequestId:138858314 CorrelationId: 631feb52-c207-44a7-9fa9-e0455d94560e

Thank you.

Please let me know the work around.

Missing auth.ts file after 1.0.5 release

ERROR in ../node_modules/nativescript-azure-auth/azure-auth.ts
Module build failed (from ../node_modules/@ngtools/webpack/src/index.js):
Error: C:\internalcomms\invisionapp\node_modules\nativescript-azure-auth\azure-auth.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files'
or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format (https://goo.gl/jB3GVv).
at NativeScriptAngularCompilerPlugin.getCompiledFile (C:\internalcomms\invisionapp\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:844:23)
at NativeScriptAngularCompilerPlugin.getCompiledFile (C:\internalcomms\invisionapp\node_modules\nativescript-dev-webpack\plugins\NativeScriptAngularCompilerPlugin.js:28:26)
at plugin.done.then (C:\internalcomms\invisionapp\node_modules@ngtools\webpack\src\loader.js:41:31)
at process._tickCallback (internal/process/next_tick.js:68:7)
@ ./app/services/azure.service.ts 5:0-52 17:31-40 23:35-44
@ ./app/app.module.ts
@ ./main.ts

During build: "adal.ios.d.ts(492,21): error TS2503: Cannot find ..."

While running on iOS (emulator), I get A LOT of errors regarding adal.ios.d.ts.

node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(4,41): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(28,47): error TS2552: Cannot find name 'NSObject'. Did you mean 'Object'?
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(32,75): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(32,93): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(34,107): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(34,125): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(36,111): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(36,129): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(38,143): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(38,161): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(40,40): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(42,75): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(48,17): error TS2304: Cannot find name 'NSUUID'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(56,20): error TS2304: Cannot find name 'UIViewController'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(60,11): error TS2304: Cannot find name 'UIWebView'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(62,73): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(62,91): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(64,94): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(64,112): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(68,116): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(70,122): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(72,144): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(74,110): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(76,150): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(78,164): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(80,158): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(82,116): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(84,136): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(86,96): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(86,114): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(88,128): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(88,146): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(91,45): error TS2304: Cannot find name 'NSError'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(95,73): error TS2304: Cannot find name 'NSDictionary'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(97,61): error TS2304: Cannot find name 'NSFileProviderItem'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(110,50): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(116,63): error TS2304: Cannot find name 'NSURL'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(118,90): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(118,108): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(120,47): error TS2304: Cannot find name 'NSHTTPURLResponse'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(120,73): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(120,91): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(124,32): error TS2304: Cannot find name 'NSDictionary'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(129,46): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(139,26): error TS2304: Cannot find name 'NSUUID'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(161,48): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(189,32): error TS2304: Cannot find name 'NSObjectProtocol'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(191,23): error TS2304: Cannot find name 'NSDictionary'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(283,44): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(301,18): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(301,36): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(301,79): error TS2304: Cannot find name 'NSArray'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(305,53): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(305,71): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(307,75): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(307,93): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(309,49): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(309,67): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(311,52): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(311,70): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(314,32): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(328,95): error TS2304: Cannot find name 'NSDictionary'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(350,35): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(367,40): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(367,60): error TS2304: Cannot find name 'NSCopying'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(367,71): error TS2304: Cannot find name 'NSSecureCoding'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(389,14): error TS2304: Cannot find name 'NSData'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(395,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(397,21): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(397,39): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(399,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(401,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(410,40): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(410,60): error TS2304: Cannot find name 'NSCopying'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(430,21): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(430,39): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(448,41): error TS2304: Cannot find name 'NSObject'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(448,61): error TS2304: Cannot find name 'NSCopying'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(448,72): error TS2304: Cannot find name 'NSSecureCoding'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(456,65): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(456,83): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(458,22): error TS2304: Cannot find name 'NSDictionary'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(490,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(492,21): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(492,39): error TS2503: Cannot find namespace 'interop'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(494,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(496,26): error TS2304: Cannot find name 'NSCoder'.
node_modules/nativescript-azure-auth/platforms/ios/typings/adal.ios.d.ts(499,43): error TS2304: Cannot find name 'NSObject'.

Error: Uncaught (in promise): Error: java.lang .NullPointerException:

I can't seem to figure out why this error is occurring. It happens as soon as I add the plugin to my nativescript app, and disappears when I remove the plugin. Do you have any idea? I removed node_modules, platforms, hooks, and reinstalled with the same result.

JS: ERROR Error: Uncaught (in promise): Error: java.lang
.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Con
text.getTheme()' on a null object reference
JS:     android.app.AlertDialog.resolveDialogTheme(Alert
Dialog.java:224)
JS:     android.app.AlertDialog$Builder.<init>(AlertDial
og.java:454)
JS:     com.tns.Runtime.callJSMethodNative(Native Method
)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runti
me.java:1203)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:10
83)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:1070)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:1050)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:1042)
JS:     com.tns.NativeScriptActivity.onCreate(NativeScri
ptActivity.java:19)
JS:     android.app.Activity.performCreate(Activity.java
:7009)
JS:     android.app.Activity.performCreate(Activity.java
:7000)
JS:     android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1214)
JS:     android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2731)

RedirectURL?

Hello,
What do I need to use for redirect URL?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.