
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。