Skip to content

Commit

Permalink
Refactor Google authentication code to store and restore validation v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
coji committed Jan 19, 2024
1 parent d2d4086 commit ac3b89c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/libs/google-auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { redirect } from '@remix-run/react'

const setStateAndNonce = async (
// 検証用の値をローカルストレージに保存する
const storeValidationValue = async (
key: string,
data: { state: string; nonce: string },
) => {
await localStorage.setItem(key, JSON.stringify(data))
}

const getStateAndNone = async (key: string) => {
// 検証用の値をローカルストレージから取得し、削除する
const restoreValidationValue = async (key: string) => {
const data = await localStorage.getItem(key)
if (!data) {
throw new Error('state がありません')
Expand Down Expand Up @@ -79,7 +81,7 @@ export const createGoogleAuthenticator = ({
state: String(Math.random()),
nonce: String(Math.random()),
}
await setStateAndNonce('state', validation)
await storeValidationValue('v', validation)

// 認可 URL にリダイレクトさせる。成功するとコールバックURLにリダイレクトされる
throw redirect(
Expand All @@ -91,7 +93,7 @@ export const createGoogleAuthenticator = ({
const params = new URLSearchParams(url.hash.slice(1))

// state のチェック
const validation = await getStateAndNone('state')
const validation = await restoreValidationValue('v')
if (validation.state !== params.get('state')) {
throw new Error('state が一致しません')
}
Expand All @@ -102,12 +104,11 @@ export const createGoogleAuthenticator = ({
throw new Error('IDトークンがありません')
}

// id トークンに含まれる nonce のチェック
// id トークンのペイロードに含まれる nonce のチェック
const jsonPayload = atob(
idToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'),
)
const payload = JSON.parse(jsonPayload)
if (payload.nonce !== validation.nonce) {
if (JSON.parse(jsonPayload).nonce !== validation.nonce) {
throw new Error('nonce が一致しません')
}

Expand Down

0 comments on commit ac3b89c

Please sign in to comment.