Skip to content

Commit a423d63

Browse files
committed
review fixes
1 parent 520e20e commit a423d63

7 files changed

Lines changed: 31 additions & 21 deletions

File tree

samples/react-native/login-pkce/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ type IdTokenClaims = {
3636
};
3737

3838
function decodeIdToken(idToken: string): IdTokenClaims {
39-
const [, payload] = idToken.split(".");
40-
if (!payload) return {};
41-
const b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
42-
const padded = b64.padEnd(b64.length + ((4 - (b64.length % 4)) % 4), "=");
43-
return JSON.parse(atob(padded)) as IdTokenClaims;
39+
try {
40+
const [, payload] = idToken.split(".");
41+
if (!payload) return {};
42+
const b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
43+
const padded = b64.padEnd(b64.length + ((4 - (b64.length % 4)) % 4), "=");
44+
return JSON.parse(atob(padded)) as IdTokenClaims;
45+
} catch {
46+
// Malformed / opaque token — fall through to the default welcome message
47+
return {};
48+
}
4449
}
4550

4651
function formatLocal(iso: string): string {

samples/react-native/login-pkce/android/app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ android {
8787
targetSdkVersion rootProject.ext.targetSdkVersion
8888
versionCode 1
8989
versionName "1.0"
90-
manifestPlaceholders = [appAuthRedirectScheme: "com.secureauth.quickstart"]
90+
// Use put() (or +=) instead of `=` so we don't wipe placeholders
91+
// injected upstream by the React Native gradle plugin (e.g. usesCleartextTraffic).
92+
manifestPlaceholders["appAuthRedirectScheme"] = "com.secureauth.quickstart"
9193
}
9294
signingConfigs {
9395
debug {

samples/react-native/login-pkce/ios/Quickstart/Info.plist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
<key>NSAllowsLocalNetworking</key>
4747
<true/>
4848
</dict>
49-
<key>NSLocationWhenInUseUsageDescription</key>
50-
<string></string>
5149
<key>RCTNewArchEnabled</key>
5250
<true/>
5351
<key>UILaunchStoryboardName</key>

samples/react-native/token-refresh/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ type IdTokenClaims = {
4545
};
4646

4747
function decodeIdToken(idToken: string): IdTokenClaims {
48-
const [, payload] = idToken.split(".");
49-
if (!payload) return {};
50-
const b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
51-
const padded = b64.padEnd(b64.length + ((4 - (b64.length % 4)) % 4), "=");
52-
return JSON.parse(atob(padded)) as IdTokenClaims;
48+
try {
49+
const [, payload] = idToken.split(".");
50+
if (!payload) return {};
51+
const b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
52+
const padded = b64.padEnd(b64.length + ((4 - (b64.length % 4)) % 4), "=");
53+
return JSON.parse(atob(padded)) as IdTokenClaims;
54+
} catch {
55+
// Malformed / opaque token — fall through to the default welcome message
56+
return {};
57+
}
5358
}
5459

5560
function welcomeMessage(...tokens: (string | null | undefined)[]): string {

samples/react-native/token-refresh/android/app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ android {
8787
targetSdkVersion rootProject.ext.targetSdkVersion
8888
versionCode 1
8989
versionName "1.0"
90-
manifestPlaceholders = [appAuthRedirectScheme: "com.secureauth.quickstart"]
90+
// Use put() (or +=) instead of `=` so we don't wipe placeholders
91+
// injected upstream by the React Native gradle plugin (e.g. usesCleartextTraffic).
92+
manifestPlaceholders["appAuthRedirectScheme"] = "com.secureauth.quickstart"
9193
}
9294
signingConfigs {
9395
debug {

samples/react-native/token-refresh/ios/Quickstart/Info.plist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
<key>NSAllowsLocalNetworking</key>
4747
<true/>
4848
</dict>
49-
<key>NSLocationWhenInUseUsageDescription</key>
50-
<string></string>
5149
<key>RCTNewArchEnabled</key>
5250
<true/>
5351
<key>UILaunchStoryboardName</key>

snippets.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,15 +678,15 @@
678678
"code": " async function signIn() {\n setError(null);\n setExpired(false);\n try {\n const result = await authorize(authConfig);\n setAuthState(result);\n } catch (e) {\n setError(e instanceof Error ? e.message : String(e));\n }\n }",
679679
"file": "App.tsx",
680680
"lang": "tsx",
681-
"lines": "67-78"
681+
"lines": "72-83"
682682
},
683683
{
684684
"step": 4,
685685
"description": "Revoke the access token at the IdP and clear local auth state",
686686
"code": " async function signOut() {\n if (!authState) return;\n try {\n await revoke(authConfig, {\n tokenToRevoke: authState.accessToken,\n sendClientId: true,\n });\n } catch (e) {\n // Revocation is best-effort — proceed with local logout regardless.\n Alert.alert(\n \"Sign-out warning\",\n e instanceof Error ? e.message : String(e),\n );\n }\n setAuthState(null);\n setExpired(false);\n }",
687687
"file": "App.tsx",
688688
"lang": "tsx",
689-
"lines": "81-99"
689+
"lines": "86-104"
690690
}
691691
],
692692
"framework": "react-native",
@@ -724,23 +724,23 @@
724724
"code": " const signIn = useCallback(async () => {\n setError(null);\n try {\n const result: AuthorizeResult = await authorize(authConfig);\n const next: Tokens = {\n accessToken: result.accessToken,\n accessTokenExpirationDate: result.accessTokenExpirationDate,\n refreshToken: result.refreshToken ?? null,\n idToken: result.idToken ?? null,\n };\n setTokens(next);\n if (next.refreshToken) {\n await persistRefreshToken(next.refreshToken);\n }\n } catch (e) {\n setError(e instanceof Error ? e.message : String(e));\n }\n }, []);",
725725
"file": "App.tsx",
726726
"lang": "tsx",
727-
"lines": "80-99"
727+
"lines": "85-104"
728728
},
729729
{
730730
"step": 4,
731731
"description": "Persist the refresh token in platform secure storage (Keychain on iOS, EncryptedSharedPreferences on Android)",
732732
"code": " async function persistRefreshToken(refreshToken: string): Promise<void> {\n await Keychain.setGenericPassword(\"refreshToken\", refreshToken, {\n service: KEYCHAIN_SERVICE,\n accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,\n });\n }\n\n async function loadRefreshToken(): Promise<string | null> {\n const credentials = await Keychain.getGenericPassword({\n service: KEYCHAIN_SERVICE,\n });\n return credentials ? credentials.password : null;\n }\n\n async function clearRefreshToken(): Promise<void> {\n await Keychain.resetGenericPassword({ service: KEYCHAIN_SERVICE });\n }",
733733
"file": "App.tsx",
734734
"lang": "tsx",
735-
"lines": "102-120"
735+
"lines": "107-125"
736736
},
737737
{
738738
"step": 5,
739739
"description": "Exchange the refresh token for a new access token; on failure clear local state and require re-login",
740740
"code": " const refreshTokens = useCallback(async () => {\n setError(null);\n try {\n const stored = tokens?.refreshToken ?? (await loadRefreshToken());\n if (!stored) {\n setError(\"No refresh token available. Sign in first.\");\n return;\n }\n const result: RefreshResult = await refresh(authConfig, {\n refreshToken: stored,\n });\n const next: Tokens = {\n accessToken: result.accessToken,\n accessTokenExpirationDate: result.accessTokenExpirationDate,\n refreshToken: result.refreshToken ?? stored,\n // Refresh responses don't always include a new id_token — keep the\n // previous one so the welcome message stays populated.\n idToken: result.idToken ?? tokens?.idToken ?? null,\n };\n setTokens(next);\n if (next.refreshToken && next.refreshToken !== stored) {\n await persistRefreshToken(next.refreshToken);\n }\n } catch (e) {\n // Refresh tokens can be revoked or expire — clear local state and surface the error.\n await clearRefreshToken();\n setTokens(null);\n setError(\n e instanceof Error\n ? `Refresh failed (${e.message}). Sign in again.`\n : `Refresh failed. Sign in again.`,\n );\n }\n }, [tokens]);",
741741
"file": "App.tsx",
742742
"lang": "tsx",
743-
"lines": "123-158"
743+
"lines": "128-163"
744744
}
745745
],
746746
"framework": "react-native",

0 commit comments

Comments
 (0)