nativescript-azure-auth
一个围绕微软 Azure 活动目录认证库本地库的 nativescript 封装
npm i --save nativescript-azure-auth

Azure 认证

一个围绕微软 Azure 活动目录认证库本地库的 nativescript 封装 ADAL for iOSADAl for Android.

版本

Current version: 1.0.5

安装

tns plugin add nativescript-azure-auth

使用 - Angular

创建一个使用此库的认证服务。

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();
}

}

登录和注销可以在组件中使用。

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();
}
}

最好将 getToken 用作拦截器,这样您可以在向受保护的 Rest API 发出 http 请求时确保将最新的 访问令牌 附加到您的标题中。

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>

打开一个 webview,重定向到 login.microsoft.com。

clearCache 参数当设置为 true 时清除 ADAL tokenCache。默认情况下 clearCache 设置为 true。

public getToken(): Promise<string>

在不需要用户交互的情况下从 Azure 活动目录 获取令牌,使用存储在相应本地库中的刷新令牌。最好与拦截器或创建需要令牌的 http 请求一起使用。

public clearCache(): void

清除在实例化此库时设置的 clientId 的整个令牌缓存。

贡献

感谢 James Browning 提供的帮助。

许可

Apache 许可证版本 2.0,2004 年 1 月