
Appauth สำหรับ JavaScript เป็นลูกค้า SDK สำหรับลูกค้าสาธารณะสำหรับการสื่อสารกับ OAuth 2.0 และ OpenID Connect ผู้ให้บริการตามแนวทางปฏิบัติที่ดีที่สุด RFC 8252 - OAuth 2.0 สำหรับแอพดั้งเดิม ไลบรารีได้รับการออกแบบมาเพื่อใช้ใน Web Apps ชัน Node.js CLI Chrome Apps และแอพพลิเคชั่นที่ใช้ Electron หรือเฟรมเวิร์กที่คล้ายกัน
มันมุ่งมั่นที่จะแมปคำขอและการตอบสนองของข้อกำหนดเหล่านั้นโดยตรงในขณะที่ทำตามรูปแบบสำนวนของภาษาการใช้งาน
ห้องสมุดยังรองรับส่วนขยาย PKCE ไปยัง OAuth ซึ่งถูกสร้างขึ้นเพื่อรักษาความปลอดภัยรหัสการอนุญาตในลูกค้าสาธารณะเมื่อใช้การเปลี่ยนเส้นทาง URI แบบกำหนดเอง ห้องสมุดเป็นมิตรกับส่วนขยายอื่น ๆ (มาตรฐานหรืออื่น ๆ ) ที่มีความสามารถในการจัดการพารามิเตอร์เพิ่มเติมในคำขอและการตอบสนองทั้งหมด
แอปพลิเคชันตัวอย่างที่ใช้ไลบรารีรวมอยู่ในโฟลเดอร์ src/node_app และที่ https://github.com/googleamples/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
} ) ;ไคลเอนต์นี้เขียนด้วย typeScript
ติดตั้งโหนดเวอร์ชันล่าสุด NVM (แนะนำตัวจัดการเวอร์ชันโหนด)
ใช้ nvm install เพื่อติดตั้งเวอร์ชัน node.js ที่แนะนำ
ดาวน์โหลด Visual Studio Code เวอร์ชันล่าสุดจากที่นี่
แอพนี้ใช้ npm เพื่อจัดเตรียมการพึ่งพา
git clone ไลบรารี AppAuthJS และไปที่โฟลเดอร์รูทของโครงการที่มีไฟล์ package.jsonnpm install เพื่อติดตั้ง Dev และ Project All Project นั่นมัน! ตอนนี้คุณพร้อมที่จะเริ่มทำงานกับ AppAuthJS
โครงการใช้สคริปต์ npm เพื่อทำให้เวิร์กโฟลว์การพัฒนาอัตโนมัติ สคริปต์เหล่านี้มีให้บริการผ่านไฟล์ package.json
รวมสคริปต์ต่อไปนี้:
npm run-script compile หรือ tsc จะรวบรวมไฟล์ TypeScript ทั้งหมดของคุณ ไฟล์ที่คอมไพล์ทั้งหมดเข้าสู่โฟลเดอร์ built/
npm run-script watch หรือ tsc --watch จะรวบรวมไฟล์ typeScript ของคุณในโหมด watch แนะนำหากคุณต้องการรับข้อเสนอแนะอย่างต่อเนื่อง
npm run-script build-app สร้างไฟล์ Output bundle.js ในไดเรกทอรี built/ ซึ่งรวมถึงไลบรารี AppAuthJS แบบเต็มรวมถึงการพึ่งพาทั้งหมด
บทบัญญัติ npm test นักวิ่งทดสอบ Karma เพื่อเรียกใช้การทดสอบหน่วยทั้งหมด การทดสอบทั้งหมดเขียนขึ้นโดยใช้จัสมิน ใน การดีบัก การทดสอบของคุณให้คลิกที่ปุ่ม Debug ใน Karma Test Runner เพื่อดูแหล่งที่มาของการทดสอบจริง คุณสามารถแนบจุดพักที่นี่
npm run-script app สร้างแอพทดสอบบนเว็บเซิร์ฟเวอร์ท้องถิ่น นี่คือแอพ end-to-end ที่ใช้ AppAuthjs และเป็นการสาธิตเกี่ยวกับวิธีการใช้ห้องสมุด
npm run-script node-app สร้างแอปตัวอย่าง Node.js CLI นี่คือแอพ end-to-end ที่ใช้ appauthjs ในบริบท node.js