
Appauth for JavaScriptは、ベストプラクティスRFC 8252 -OAUTH 2.0をネイティブアプリ用のOAUTH 2.0およびOpenID Connectプロバイダーと通信するためのパブリッククライアント向けのクライアントSDKです。ライブラリは、 Web Apps 、 Node.js CLIアプリケーション、 Chrome Apps 、 Electronまたは同様のフレームワークを使用するアプリケーションで使用するように設計されています。
実装言語の慣用的なスタイルに従って、これらの仕様のリクエストと応答を直接マッピングするよう努めています。
ライブラリは、カスタムURIスキームリダイレクトが使用されたときにパブリッククライアントの承認コードを確保するために作成されたOAuthへのPKCE拡張もサポートしています。ライブラリは、すべてのプロトコル要求と応答で追加のパラメーターを処理する機能を備えた他の拡張機能(標準またはその他)に優しいです。
ライブラリを使用したアプリケーションの例はsrc/node_appフォルダーとhttps://github.com/goolsamples/appauth-js-electron-sampleに含まれています。
Appauthは、独自のトークン交換を実行する必要があるAuthorization Serverとの手動相互作用をサポートしています。この例は、手動交換を実行します。
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
} ) ;このクライアントはTypeScriptで書かれています。
ノードの最新バージョンをインストールします。 NVM(ノードバージョンマネージャーを強くお勧めします)。
nvm installを使用して、推奨node.jsバージョンをインストールします。
ここからVisual Studioコードの最新バージョンをダウンロードしてください。
このアプリは、 npmを使用してIT依存関係を提供します。
git clone AppAuthJSライブラリをクローンし、 package.jsonファイルを含むプロジェクトのルートフォルダーに移動します。npm installすべての開発とプロジェクトの依存関係をインストールします。それでおしまい!これで、 AppAuthJSの作業を開始する準備ができました。
このプロジェクトは、 npmスクリプトを使用して開発ワークフローを自動化します。これらのスクリプトは、 package.jsonファイルを介して利用可能になります。
次のスクリプトが含まれています。
npm run-script compileまたはtsc 、すべてのTypeScriptファイルをコンパイルします。コンパイルされたすべてのファイルはbuilt/フォルダーに移動します。
npm run-script watchまたはtsc --watch 、 watch ModeでTypeScriptファイルをコンパイルします。継続的なフィードバックを取得する場合はお勧めします。
npm run-script build-app built/ディレクトリで出力bundle.jsファイルを生成します。これには、すべての依存関係を含む完全なAppAuthJSライブラリが含まれます。
npm testすべてのユニットテストを実行するためにKarmaテストランナーを規定しています。すべてのテストは、ジャスミンを使用して記述されます。テストをデバッグするには、KarmaテストランナーのDebugボタンをクリックして、テストの実際のソースを確認します。ここでブレークポイントを添付できます。
npm run-script appローカルWebサーバーにテストアプリを構築します。これは、appauthjsを使用するエンドツーエンドアプリであり、ライブラリの使用方法に関するデモです。
npm run-script node-app node.js cliサンプルアプリを構築します。これは、node.jsコンテキストでappauthjsを使用するエンドツーエンドアプリです。