Open7
Amplify LibraryのAuthのメモ
Auth.currentAuthenticatedUser
- ストレージとメモリが同期するのを待つ(あとでより詳しく読む)
- デフォルトではCookieをストレージとして使っている。
- Federationしている場合の処理
- ストレージからFederationの情報を取得
- ストレージに
'aws-amplify-federatedInfo'というキー名で保存 - JSONで、userとtokenというフィールドがある
- ストレージに
- 情報がある場合は、その情報を
this.userとし、this.userを返す
- ストレージからFederationの情報を取得
- Fetderationしていない場合は、
this.currentUserPoolUserからユーザー情報を取得し、その返り値をthis.userとする&返す
Auth.currentUserPoolUser
引数の方は以下。
export interface CurrentUserOpts {
bypassCache: boolean;
}
- ストレージとメモリが同期するのを待つ
-
this.isOAuthInProgress() === trueの場合、終わるのを待つ or タイムアウトするまで待つ- 終わるのを待つ =
Hub.listen('auth', callback)で'cognitoHostedUI'or'cognitoHostedUI_failure'が来るのを待つ - タイムアウトしても普通にresolveしているので、タイムアウトした場合にどういうことが発生するのかは謎。
- 終わるのを待つ =
-
this.userPool.getCurrentUser()でユーザー(user)を取得- ユーザーが居なければ、Rejectする
- ユーザーからセッションを取得
-
bypassCacheが有効な場合はクレデンシャルをクリア(this.Credentials.clear()) - セッションからアクセストークンを取得し、スコープを取得
- スコープに管理者スコープ(
aws.cognito.signin.user.admin)が含まれる場合:-
user.userData()を実行、ユーザー属性とprefferedMFAを取得し、user.attributes,user.prefferedMFAに設定 - 含まれない場合は何もせず
userをresolveする。
-
private isOAuthInProgress(): boolean
-
this. oAuthFlowInProgressを返しているだけ-
this. oAuthFlowInProgressに代入している部分-
private async _handleAuthResponse- 呼び出されたときにtrueを代入し、finallyでfalseを代入
-
_handleAuthResponseはAuth.configure時にurlListenerのコールバックでコールされている - Amplifyがホストしている認証用のUIを利用するときに、URLが変わった=そのUIに遷移したという判定しているのではないかと予測。
-
-
urlListener
- ブラウザでは引数のコールバックに
window.location.hrefを渡しているだけ。 - Node.jsの場合は何もしない
- React Native用のコードもある(読んでない)
this.userPool
- CognitoUserPoolのインスタンス
ストレージ
Auth.configureでthis._storageにインスタンス化。
// https://github.com/aws-amplify/amplify-js/blob/6caccc7b4c06cf034df98bf42a265e4fff9be12f/packages/auth/src/Auth.ts#L162-L176
if (!this._config.storage) {
// backward compatability
if (cookieStorage) this._storage = new CookieStorage(cookieStorage);
else {
this._storage = config.ssr
? new UniversalStorage()
: new StorageHelper().getStorage();
}
} else {
if (!this._isValidAuthStorage(this._config.storage)) {
logger.error('The storage in the Auth config is not valid!');
throw new Error('Empty storage object');
}
this._storage = this._config.storage;
}
- 後方互換性のため、config.cookieStorageがある場合は
CookieStorageのインスタンス - SSRが有効な場合は、
UniversalStorageのインスタンス - それ以外は
new StorageHelper().getStorage()
this._storageにsync関数がある場合は実行。
// https://github.com/aws-amplify/amplify-js/blob/6caccc7b4c06cf034df98bf42a265e4fff9be12f/packages/auth/src/Auth.ts#L178-L181
this._storageSync = Promise.resolve();
if (typeof this._storage['sync'] === 'function') {
this._storageSync = this._storage['sync']();
}
CookieStorage
-
amazon-cognito-identity-jsにある - getItem, setItem, removeItem, clear
- window.Cookieを使って書き込み・読み込み・削除
UniversalStorage
-
universal-cookieを使っているので、Node.js/Browser両方で動作 - getItemはローカル変数を参照
- setItem時、特定のキーだけCookieに書き込み、それ以外はローカル変数に書き込み
new StorageHelper().getStorage()
localStorage or メモリー
this._storage.sync
- ↑の3つにはなかった。
_isValidAuthStorage
- getItem/setItem/removeItem/clearがあるかどうか
CognitoUserPool
TODO