Skip to content

Commit 7bf6609

Browse files
authored
[ApiAuth] Switches back to use code+PKCE (#12375)
* Move SPA flows to use code + pkce * Updates OIDC dependency to 1.9-beta1
1 parent d8911c1 commit 7bf6609

File tree

20 files changed

+1981
-950
lines changed

20 files changed

+1981
-950
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace ApiAuthSample.Controllers
5+
{
6+
public class ConfigurationController : ControllerBase
7+
{
8+
private readonly IClientRequestParametersProvider _clientRequestParametersProvider;
9+
10+
public ConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider)
11+
{
12+
_clientRequestParametersProvider = clientRequestParametersProvider;
13+
}
14+
15+
[HttpGet("/_configuration/{clientId}")]
16+
public IActionResult GetClientParameters(string clientId)
17+
{
18+
var parameters = _clientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
19+
if (parameters == null)
20+
{
21+
return BadRequest($"Parameters for client '{clientId}' not found.");
22+
}
23+
else
24+
{
25+
return Ok(parameters);
26+
}
27+
}
28+
}
29+
}

src/Identity/ApiAuthorization.IdentityServer/samples/ApiAuthSample/Pages/Index.cshtml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@
1414
</head>
1515
<body>
1616
<h1>ApiAuthSample SPA client</h1>
17-
<button id="login">Login</button>
17+
<button id="login" disabled>Login</button>
1818
<button id="logout" disabled>Logout</button>
1919
<button id="call-api" disabled>Call API</button>
2020
<div id="login-result"></div>
2121
<div id="api-result"></div>
2222
<script src="/js/oidc-client.js"></script>
23-
<script id="apiauth" type="text/javascript" asp-apiauth-parameters="ApiAuthSampleSPA">
24-
let $data = document.querySelector("#apiauth");
25-
let configuration = {};
26-
for (let key in $data.dataset) {
27-
configuration[key] = $data.dataset[key];
28-
}
29-
30-
let mgr = new Oidc.UserManager(configuration);
31-
</script>
3223
<script src="js/app.js"></script>
24+
<script id="apiauth" type="text/javascript">
25+
initializeApplication();
26+
</script>
3327
</body>
3428
</html>

src/Identity/ApiAuthorization.IdentityServer/samples/ApiAuthSample/wwwroot/js/app.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-

1+
var ids = {
2+
login: 'login',
3+
logout: 'logout',
4+
callApi: 'call-api',
5+
loginResult: 'login-result',
6+
apiResults: 'api-result'
7+
};
8+
9+
let mgr = undefined;
10+
211
function invokeLogin() {
312
// Redirects to the Authorization Server for sign in.
413
return mgr.signinRedirect();
@@ -43,19 +52,14 @@ async function callApi() {
4352

4453
// Code to update the UI
4554

46-
if (window.location.hash) {
47-
handleAuthorizationServerCallback();
48-
window.location.hash = '';
55+
if (window.location.hash || window.location.search) {
56+
initializeApplication()
57+
.then(() => {
58+
handleAuthorizationServerCallback();
59+
window.location.hash = '';
60+
});
4961
}
5062

51-
let ids = {
52-
login: 'login',
53-
logout: 'logout',
54-
callApi: 'call-api',
55-
loginResult: 'login-result',
56-
apiResults: 'api-result'
57-
};
58-
5963
document.onreadystatechange = function () {
6064
if (document.readyState === 'complete') {
6165
let login = document.getElementById(ids.login);
@@ -68,6 +72,20 @@ document.onreadystatechange = function () {
6872
}
6973
};
7074

75+
async function initializeApplication() {
76+
const response = await fetch('_configuration/ApiAuthSampleSPA');
77+
const configuration = await response.json();
78+
mgr = new Oidc.UserManager(configuration);
79+
80+
enableLoginButton();
81+
82+
function enableLoginButton() {
83+
const login = document.querySelector('#login');
84+
login.disabled = false;
85+
}
86+
}
87+
88+
7189
function updateUserUI(user, error) {
7290
let loginResults = document.getElementById(ids.loginResult);
7391
let heading = document.createElement('h2');

0 commit comments

Comments
 (0)