Open7

Amplify LibraryのAuthのメモ

pirosikickpirosikick

Auth.currentAuthenticatedUser

ソース

  • ストレージとメモリが同期するのを待つ(あとでより詳しく読む)
    • デフォルトではCookieをストレージとして使っている。
  • Federationしている場合の処理
    • ストレージからFederationの情報を取得
      • ストレージに'aws-amplify-federatedInfo'というキー名で保存
      • JSONで、userとtokenというフィールドがある
    • 情報がある場合は、その情報をthis.userとし、this.userを返す
  • Fetderationしていない場合は、this.currentUserPoolUserからユーザー情報を取得し、その返り値をthis.userとする&返す
pirosikickpirosikick

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する。
pirosikickpirosikick

private isOAuthInProgress(): boolean

ソース

  • this. oAuthFlowInProgressを返しているだけ
    • this. oAuthFlowInProgressに代入している部分
      • private async _handleAuthResponse
        • 呼び出されたときにtrueを代入し、finallyでfalseを代入
      • _handleAuthResponseはAuth.configure時にurlListenerのコールバックでコールされている
      • Amplifyがホストしている認証用のUIを利用するときに、URLが変わった=そのUIに遷移したという判定しているのではないかと予測。
pirosikickpirosikick

urlListener

ソース

  • ブラウザでは引数のコールバックにwindow.location.hrefを渡しているだけ。
  • Node.jsの場合は何もしない
  • React Native用のコードもある(読んでない)
pirosikickpirosikick

ストレージ

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

ソース

new StorageHelper().getStorage()

ソース

localStorage or メモリー

this._storage.sync

  • ↑の3つにはなかった。

_isValidAuthStorage

ソース

  • getItem/setItem/removeItem/clearがあるかどうか