ã¦ã¼ã¶ã¼ãéå®ããwebãµã¤ãã»webã¢ããªã¯ãã°ã¤ã³ã®ä»çµã¿ãå¿
è¦ã«ãªã.
AWS cognito, ç¹ã«User Poolsã®æ©è½ã§ãããå®è£
ã§ãã.
Cognitoã¨ã¯ä½ãï¼ã¨ããæ¹ã¯ãã¡ãä¸èªãæ¨å¥¨.
tarepan.hatenablog.com
Cognitoãç¨ããã»ãã·ã§ã³ç®¡çã®å¤§æ
- ã¤ã³ã¿ã¼ããããä»ããååèªè¨¼ãèªè¨¼æ å ±ã®ãã¼ã«ã« (ãã©ã¦ã¶) ã¸ã®ä¿å
- åwebãã¼ã¸/ã¢ããªé·ç§»ã§ãã¼ã«ã«èªè¨¼æ å ±ã®ãã§ãã¯ãä¸æ£ãªãããããã¼ã¸/ãã°ã¤ã³ç»é¢ã¸å¼·å¶ãªã³ã¯
ååèªè¨¼æã®ãã¼ã¿ããã¼ã«ã«ã¸èªåä¿åãããå¿
è¦ã«å¿ãã¦ãã¼ã¸é·ç§»æãªã©ã«èªã¿åºãé¢æ°ã§ããããã§ã㯠(validation)ãæªãã°ã¤ã³ã»æéåãçã®å ´åã¯ãã®ãã¼ã¸ãã追ãåºã.
AWS Cognito UserPoolsãå©ç¨ããã°ãé¢æ°ãæ°åå©ãã ãã§ãã®ä»çµã¿ãå®è£
ã§ãã, ãããã Amazon.
詳細
- JWT (Json Web Token)
- èªè¨¼ããã¼
ã®ç¥èãããã¨è¯ãã
èªè¨¼æã«localStorageã¸JwtTokenãã»ãããã"ãã®å¾"ã¯getSessionã§tokenã®åãåºãã»å©ç¨æéã®validationãããä¸æ£ãªå ´åã¯getSessionã®callbackå
ã§ãã°ã¤ã³ãã¼ã¸ã¸é£ã°ã.
"ãã®å¾"ã§ã¯ãããã¯ã¼ã¯ãä»ãããã§ãã¯ã¯ãã¦ãªããã.
ãã°ã¢ã¦ãæã«ã¯localStorageã¨sessionå¤æ°ã空ã«ãã¦ã.
åå : ãããã¯ã¼ã¯ä»ããèªè¨¼
2åç®ä»¥é : localStorageã®tokensãvalidationãã¦è¨±å¯
ã¦ã¼ã¶ã¼èªè¨¼ã¨ãã¼ã¸ãã¾ããã ãã°ã¤ã³ã®éè¦ã³ã¼ã
UserPoolã¤ã³ã¹ã¿ã³ã¹ã®ä½æ
new CognitoIdP.CognitoUserPool(data)
- @param {object} data / Creation options.
CognitoUser.authenticateUser
å é¨ã§authRequest (CognitoIdPãã¤ãã£ãé¢æ°) ãå©ããæå®ã®èªè¨¼ããã»ã¹ãééããã
- signInUserSessionã¸ã®ã»ãã
- localStorageã¸ã®cache
ãå®è¡
// amazon-cognito-identity-js/src/CognitoUser.js // most important: line 225 - 227 export default class CognitoUser { ... authenticateUser(authDetails, callback) { ... this.client.makeUnauthenticatedRequest('initiateAuth', { ... }, (err, data) => { ... this.client.makeUnauthenticatedRequest('respondToAuthChallenge', { ...}, (errAuthenticate, dataAuthenticate) => { ... this.signInUserSession = this.getCognitoUserSession(dataAuthenticate.AuthenticationResult); this.cacheTokens(); ... }); }); }} // line 77 constructor(data){ ... this.client = data.Pool.client; } // amazon-cognito-identity-js/src/CognitoUserPool.js export default class CognitoUserPool { ... constructor(data) { ... this.client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region }); }}
getCurrentUser
CognitoUserPool.getCurrentUser()
æå¾ã«èªè¨¼ããã¦ã¼ã¶ã¼åãlocalStorageããåãåºããCognitoUserã«ãã¦è¿ã.ããªãå ´åã¯null.
localStorageã¸ã®ç»é²ã¿ã¤ãã³ã°ã¯ CognitoUser.cacheToken() å®è¡æã®ã¿.
å®è¡ã¿ã¤ãã³ã°ã¯ CognitoUser.authenticateUser() ã®ã¿.
()=>CognitoUser(LastAuthUser)
// amazon-cognito-identity-js/src/CognitoUser.js // important: line 788 - 800 export default class CognitoUser { ... cacheTokens() { const keyPrefix = `CognitoIdentityServiceProvider.${this.pool.getClientId()}`; ... const lastUserKey = `${keyPrefix}.LastAuthUser`; ... storage.setItem(lastUserKey, this.username); } ... }
getSession
validãªtokensãæ¤åºããå ´åã«callbackãå®è¡ããã.
cognitoUser.getSession(callback)
callback: (err, signInUserSession)=>{}
cognitoUserã«validãªsignInUserSessionãããã°å³callbackå®è¡.
ãªãå ´åã¯ã¦ã¼ã¶ã¼åããã¨ã«localStorageããtokenãåå¾ãCognitoUserSessionã«å
¥ããvalidãªãcallbackå®è¡.
æéåã (invalid) ãªãrefreshTokenãç¨ãã¦åå¾ãç´ãããã®å¾ callback.
ä»ã®å ´åã¯Errorã§callbackå®è¡.
CognitoUserSession
IdToken, AccessToken, RefreshTokenã®å ¥ãç©. validation checké¢æ° (isValid) ãå®è£ .
AWS Cognito Identityã¨ã®é¢ä¿
ã¦ã¼ã¶ã¼ç»é²ã»ãã°ã¤ã³ã»ã»ãã·ã§ã³ç®¡ç㯠(åºæ¬çã«) User Poolsä¸ã§å®çµãã.
Identity PoolãFederated Identityã®ä»çµã¿ã¯ãä»ã®AWS resourceã¸ã®ã¢ã¯ã»ã¹çã«å¿
è¦ã«ãªã.
amazon-cognito-identity-js
å ¨ã¦ã¯è§£æ±ºãããamazon-cognito-identity-js 㯠AWS.CognitoIdenitityServiceProviderãã¡ã½ãã追å ã§æ¡å¼µãã¦ããã
åè
Integrating User Pools with Amazon Cognito Identity - Amazon Cognito