
JavaScript的Appauth是公共客户端的客户端SDK,用于与OAuth 2.0和OpenID连接提供商进行交流,遵循最佳实践RFC 8252 -OAUTH 2.0用于本机应用程序。该库设计用于用于使用Electron或类似框架的Web Apps , Node.js CLI应用程序, Chrome Apps和应用程序。
它努力在遵循实现语言的惯用样式的同时,直接映射这些规格的请求和响应。
该库还支持对OAuth的PKCE扩展,该扩展是为了在使用自定义URI方案重定向时为公共客户端确保授权代码而创建的。该库对其他扩展名(标准或其他)友好,能够在所有协议请求和响应中处理其他参数。
使用该库的示例应用程序包含在src/node_app文件夹中,并在https://github.com/googlesamples/appauth-js-electron-sample中包含。
Appauth支持与授权服务器进行手动交互,您需要执行自己的令牌交换。此示例执行手动交换。
AuthorizationServiceConfiguration . fetchFromIssuer ( openIdConnectUrl )
. then ( response => {
log ( 'Fetched service configuration' , response ) ;
this . configuration = response ;
this . showMessage ( 'Completed fetching configuration' ) ;
} )
. catch ( error => {
log ( 'Something bad happened' , error ) ;
this . showMessage ( `Something bad happened ${ error } ` )
} ) ; this . notifier = new AuthorizationNotifier ( ) ;
// uses a redirect flow
this . authorizationHandler = new RedirectRequestHandler ( ) ;
// set notifier to deliver responses
this . authorizationHandler . setAuthorizationNotifier ( this . notifier ) ;
// set a listener to listen for authorization responses
this . notifier . setAuthorizationListener ( ( request , response , error ) => {
log ( 'Authorization request complete ' , request , response , error ) ;
if ( response ) {
this . code = response . code ;
this . showMessage ( `Authorization Code ${ response . code } ` ) ;
}
} ) ;
// create a request
let request = new AuthorizationRequest ( {
client_id : clientId ,
redirect_uri : redirectUri ,
scope : scope ,
response_type : AuthorizationRequest . RESPONSE_TYPE_CODE ,
state : undefined ,
extras : { 'prompt' : 'consent' , 'access_type' : 'offline' }
} ) ;
// make the authorization request
this . authorizationHandler . performAuthorizationRequest ( this . configuration , request ) ; this . tokenHandler = new BaseTokenRequestHandler ( ) ;
let request : TokenRequest | null = null ;
if ( this . code ) {
let extras : StringMap | undefined = undefined ;
if ( this . request && this . request . internal ) {
extras = { } ;
extras [ 'code_verifier' ] = this . request . internal [ 'code_verifier' ] ;
}
// use the code to make the token request.
request = new TokenRequest ( {
client_id : clientId ,
redirect_uri : redirectUri ,
grant_type : GRANT_TYPE_AUTHORIZATION_CODE ,
code : this . code ,
refresh_token : undefined ,
extras : extras
} ) ;
} else if ( this . tokenResponse ) {
// use the token response to make a request for an access token
request = new TokenRequest ( {
client_id : clientId ,
redirect_uri : redirectUri ,
grant_type : GRANT_TYPE_REFRESH_TOKEN ,
code : undefined ,
refresh_token : this . tokenResponse . refreshToken ,
extras : undefined
} ) ;
}
this . tokenHandler . performTokenRequest ( this . configuration , request )
. then ( response => {
// ... do something with token response
} ) ;该客户端已用打字稿编写。
安装最新版本的节点。 NVM(强烈建议使用节点版本管理器)。
使用nvm install来安装推荐的节点.js版本。
从此处下载最新版本的Visual Studio代码。
该应用使用npm来提供IT依赖关系。
git clone AppAuthJS库,然后转到包含package.json文件的项目的根文件夹。npm install以安装所有DEV和项目依赖关系。就是这样!您现在准备开始从事AppAuthJS 。
该项目使用npm脚本来自动化开发工作流程。这些脚本可通过package.json文件提供。
包括以下脚本:
npm run-script compile或tsc将编译所有打字稿文件。所有编译的文件都进入built/文件夹。
npm run-script watch或tsc --watch将在watch模式下编译您的打字稿文件。如果您想获得连续反馈,建议使用。
npm run-script build-app在built/目录中生成输出bundle.js包。这包括完整的AppAuthJS库,包括其所有依赖项。
npm test规定Karma测试跑者将运行所有单元测试。所有测试均使用茉莉花编写。要调试测试,请单击业力测试跑步者中的Debug按钮,以查看测试的实际来源。您可以在这里附加断点。
npm run-script app在本地Web服务器上构建了测试应用程序。这是一个使用appauthjs的端到端应用程序,是有关如何使用库的演示。
npm run-script node-app构建Node.js CLI示例应用程序。这是一个端到端的应用程序,它在node.js上下文中使用appauthjs。