Skip to content

Commit 0b8724a

Browse files
mattgotteinerMatt Gotteiner
andauthored
Update Authentication to use local storage and easy auth refresh (#1117)
* switch to localstorage; add refresh * run prettier * fix tests --------- Co-authored-by: Matt Gotteiner <[email protected]>
1 parent e518ab0 commit 0b8724a

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

app/backend/core/authentication.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ def get_auth_setup_for_client(self) -> dict[str, Any]:
6565
"navigateToLoginRequestUrl": False, # If "true", will navigate back to the original request location before processing the auth code response.
6666
},
6767
"cache": {
68-
"cacheLocation": "sessionStorage",
68+
# Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO between tabs.
69+
"cacheLocation": "localStorage",
70+
# Set this to "true" if you are having issues on IE11 or Edge
6971
"storeAuthStateInCookie": False,
70-
}, # Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO between tabs. # Set this to "true" if you are having issues on IE11 or Edge
72+
},
7173
},
7274
"loginRequest": {
7375
# Scopes you add here will be prompted for user consent during sign-in.

app/frontend/src/authConfig.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { IPublicClientApplication } from "@azure/msal-browser";
44

55
const appServicesAuthTokenUrl = ".auth/me";
6+
const appServicesAuthTokenRefreshUrl = ".auth/refresh";
67
const appServicesAuthLogoutUrl = ".auth/logout?post_logout_redirect_uri=/";
78

89
interface AppServicesToken {
@@ -88,18 +89,24 @@ export const getRedirectUri = () => {
8889
// Get an access token if a user logged in using app services authentication
8990
// Returns null if the app doesn't support app services authentication
9091
const getAppServicesToken = (): Promise<AppServicesToken | null> => {
91-
return fetch(appServicesAuthTokenUrl).then(r => {
92+
return fetch(appServicesAuthTokenRefreshUrl).then(r => {
9293
if (r.ok) {
93-
return r.json().then(json => {
94-
if (json.length > 0) {
95-
return {
96-
id_token: json[0]["id_token"] as string,
97-
access_token: json[0]["access_token"] as string,
98-
user_claims: json[0]["user_claims"].reduce((acc: Record<string, any>, item: Record<string, any>) => {
99-
acc[item.typ] = item.val;
100-
return acc;
101-
}, {}) as Record<string, any>
102-
};
94+
return fetch(appServicesAuthTokenUrl).then(r => {
95+
if (r.ok) {
96+
return r.json().then(json => {
97+
if (json.length > 0) {
98+
return {
99+
id_token: json[0]["id_token"] as string,
100+
access_token: json[0]["access_token"] as string,
101+
user_claims: json[0]["user_claims"].reduce((acc: Record<string, any>, item: Record<string, any>) => {
102+
acc[item.typ] = item.val;
103+
return acc;
104+
}, {}) as Record<string, any>
105+
};
106+
}
107+
108+
return null;
109+
});
103110
}
104111

105112
return null;

tests/test_authenticationhelper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_auth_setup(mock_confidential_client_success):
8080
"postLogoutRedirectUri": "/",
8181
"navigateToLoginRequestUrl": False,
8282
},
83-
"cache": {"cacheLocation": "sessionStorage", "storeAuthStateInCookie": False},
83+
"cache": {"cacheLocation": "localStorage", "storeAuthStateInCookie": False},
8484
},
8585
"loginRequest": {
8686
"scopes": [".default"],
@@ -104,7 +104,7 @@ def test_auth_setup_required_access_control(mock_confidential_client_success):
104104
"postLogoutRedirectUri": "/",
105105
"navigateToLoginRequestUrl": False,
106106
},
107-
"cache": {"cacheLocation": "sessionStorage", "storeAuthStateInCookie": False},
107+
"cache": {"cacheLocation": "localStorage", "storeAuthStateInCookie": False},
108108
},
109109
"loginRequest": {
110110
"scopes": [".default"],

0 commit comments

Comments
 (0)