Skip to content

Commit

Permalink
chore: Fix GitHub login error
Browse files Browse the repository at this point in the history
Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Jan 10, 2024
1 parent f1e7b1f commit b01571a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Functions/Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public async Task<IActionResult> GithubSignin(
Logger.Error(error);
throw new Exception(error);
}
req.Headers.TryGetValue("Referer", out var _backEnd);
string backEnd = _backEnd.ToString() ?? req.GetDisplayUrl();
req.Headers.TryGetValue("Referer", out var _backend);
string backend = _backend.Count != 0 ? _backend.First() : req.GetDisplayUrl();

string idToken = await _githubService.GetIdTokenAsync(
authorization_code: code,
redirectUri: AuthenticationService.GetRedirectUri(backEnd, "api/signin-github"));
redirectUri: AuthenticationService.GetRedirectUri(backend, "api/signin-github"));

// Treat it as an implicit flow-style URL so that my front-end can easily handle it with packages (angular-oauth2-oidc).
return new RedirectResult($"{_frontEndUri}/pages/login-redirect#state={HttpUtility.UrlEncode(state)}&access_token={idToken}&token_type=Bearer&expires_in=3599&scope=email%20profile&authuser=0&prompt=none");
Expand Down
2 changes: 1 addition & 1 deletion Services/Authentication/GithubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<ClaimsPrincipal> GetUserInfoFromTokenAsync(string token)

using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
var identity = new ClaimsIdentity("github");
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, payload.RootElement.GetProperty("id").GetString() ?? "", ClaimValueTypes.String));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, payload.RootElement.GetProperty("id").GetInt32().ToString(), ClaimValueTypes.Integer32));
identity.AddClaim(new Claim(ClaimTypes.Name, payload.RootElement.GetProperty("login").GetString() ?? "", ClaimValueTypes.String));
identity.AddClaim(new Claim(ClaimTypes.GivenName, payload.RootElement.GetProperty("name").GetString()?.Split(' ', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? "", ClaimValueTypes.String));
identity.AddClaim(new Claim(ClaimTypes.Surname, payload.RootElement.GetProperty("name").GetString()?.Split(' ', StringSplitOptions.RemoveEmptyEntries).LastOrDefault() ?? "", ClaimValueTypes.String));
Expand Down

0 comments on commit b01571a

Please sign in to comment.