-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix login sequence * Remove invalid logic * Frontend auth refactor. --------- Co-authored-by: Pavel <[email protected]>
- Loading branch information
1 parent
f617c98
commit 29b750e
Showing
9 changed files
with
68 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"redirectUri": "http://localhost:5000", | ||
"backend": "http://localhost:5001", | ||
"auth": "http://localhost:5002" | ||
"auth": "http://localhost:5002", | ||
"clientId": "fake" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export class AuthApi { | ||
constructor(private clientId: string, private redirectUri: string, private authUrl: string) {} | ||
|
||
exchangeCode = async (code: string | null): Promise<string | null> => { | ||
if (!code) return null; | ||
|
||
const body = `grant_type=authorization_code&client_id=${this.clientId}&code=${code}&redirect_uri=${this.redirectUri}`; | ||
|
||
const response = await fetch(`${this.authUrl}/oauth2/token`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | ||
}, | ||
body | ||
}); | ||
|
||
const responseBody = await response.json(); | ||
|
||
return responseBody.id_token; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
import { AuthApi } from './AuthApi'; | ||
import { | ||
getTokenFromStorage, | ||
getTokenFromUrl, | ||
getCodeFromUrl, | ||
putTokenToStorage, | ||
removeTokenFromStorage, | ||
redirectToAuthPage | ||
} from './tools'; | ||
|
||
export class Authenticator { | ||
constructor(private authUrl: string) {} | ||
constructor(private authUrl: string, private redirectUri: string, private api: AuthApi) {} | ||
|
||
getToken = () => { | ||
let token = getTokenFromStorage(); | ||
getStoredToken = (): Promise<string | null> => { | ||
return this.getToken(); | ||
}; | ||
|
||
signIn = async (): Promise<string | null> => { | ||
const token = await this.getToken(); | ||
|
||
if (!token) { | ||
token = getTokenFromUrl(); | ||
if (token) putTokenToStorage(token); | ||
redirectToAuthPage(this.authUrl, this.redirectUri); | ||
return null; | ||
} | ||
|
||
return token; | ||
}; | ||
|
||
autoSignIn = async () => { | ||
const token = getTokenFromStorage() ?? getTokenFromUrl(); | ||
signOut = (): void => removeTokenFromStorage(); | ||
|
||
return token; | ||
}; | ||
|
||
signIn = async () => { | ||
const token = this.getToken(); | ||
private getToken = async (): Promise<string | null> => { | ||
let token = getTokenFromStorage(); | ||
if (token) return token; | ||
|
||
if (!token) redirectToAuthPage(this.authUrl); | ||
token = await this.getTokenFromCode(); | ||
if (token) putTokenToStorage(token); | ||
|
||
return token; | ||
}; | ||
|
||
signOut = async () => removeTokenFromStorage(); | ||
private getTokenFromCode = async (): Promise<string | null> => { | ||
const code = getCodeFromUrl(); | ||
if (!code) return null; | ||
|
||
return this.api.exchangeCode(code); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function redirectToAuthPage(authUrl: string) { | ||
window.location.replace(`${authUrl}/login?redirect_uri=${window.location.href}&response_type=code&scope=openid`); | ||
export function redirectToAuthPage(authUrl: string, redirectUri: string) { | ||
window.location.replace(`${authUrl}/login?redirect_uri=${redirectUri}&response_type=code&scope=openid`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export interface Config { | ||
backend: string; | ||
auth: string; | ||
redirectUri: string; | ||
clientId: string; | ||
} |