Skip to content

Commit 449c83f

Browse files
committed
Implement authorization code flow
1 parent a926806 commit 449c83f

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,3 @@ code: <<der Authorization Code>>
4848

4949
In der Antwort dieses Requests ist das JWT enthalten.
5050
Dieses kann nun auf der Startseite angezeigt werden.
51-

authentication-ui/nginx.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
events {
22
}
3+
4+
env CODESPACE_NAME;
5+
36
http {
47

8+
map $CODESPACE_NAME CODESPACE_HOST {
9+
"" "localhost:6060";
10+
default "$CODESPACE_NAME-6060.app.github.dev";
11+
}
12+
513
server {
614
listen 8080;
715
root /var/www;
16+
17+
location /resources/ {
18+
proxy_pass http://keycloak:8080;
19+
}
20+
21+
location /realms {
22+
proxy_pass http://keycloak:8080;
23+
proxy_set_header Host $CODESPACE_HOST;
24+
}
825
}
926
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
events {
2+
}
3+
4+
http {
5+
6+
server {
7+
listen 8080;
8+
root /var/www;
9+
10+
location /resources/ {
11+
proxy_pass http://keycloak:8080;
12+
}
13+
14+
location /realms {
15+
proxy_pass http://keycloak:8080;
16+
proxy_set_header Host ${CODESPACE_HOST};
17+
}
18+
}
19+
}

authentication-ui/static/index.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,43 @@ <h1>Onlineshop</h1>
99
<button id="loginButton">Login</button>
1010
<div id="result"></div>
1111
<script>
12+
const authorizeEndpoint = "/realms/master/protocol/openid-connect/auth";
13+
const tokenEndpoint = "/realms/master/protocol/openid-connect/token";
14+
1215
document.getElementById("loginButton").onclick = function() {
13-
console.log("Redirect to authentication server");
16+
var redirectUri = window.location.href;
17+
var args = new URLSearchParams({
18+
client_id: "onlineshop",
19+
scope: "openid",
20+
response_type: "code",
21+
redirect_uri: redirectUri
22+
});
23+
console.log("args", args);
24+
window.location = authorizeEndpoint + "/?" + args;
1425
}
1526

1627
if (window.location.search) {
1728
var args = new URLSearchParams(window.location.search);
1829
var code = args.get("code");
1930

2031
if (code) {
21-
console.log("Authenticate with authorization code");
32+
fetch(tokenEndpoint, {
33+
method: "POST",
34+
headers: {
35+
'Content-Type': 'application/x-www-form-urlencoded',
36+
},
37+
body: new URLSearchParams({
38+
client_id: "onlineshop",
39+
scope: "openid",
40+
grant_type: "authorization_code",
41+
redirect_uri: location.href.replace(location.search, ''),
42+
code: code
43+
})
44+
}
45+
).then(response => response.json()
46+
).then(json => {
47+
document.getElementById("result").innerHTML = json.access_token;
48+
});
2249
}
2350
}
2451
</script>

docker-compose.yaml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ services:
44
ports:
55
- "6060:8080"
66
volumes:
7-
- ./authentication-ui/nginx.conf:/etc/nginx/nginx.conf
7+
- ./authentication-ui/nginx.conf.template:/etc/nginx/nginx.conf.template
88
- ./authentication-ui/static:/var/www
9+
environment:
10+
CODESPACE_NAME: ${CODESPACE_NAME:-}
11+
command: |
12+
/bin/bash -c '
13+
export CODESPACE_HOST=$([ -n "$CODESPACE_NAME" ] && echo "$CODESPACE_NAME-6060.app.github.dev" || echo "localhost:6060");
14+
envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf;
15+
exec nginx -g "daemon off;"
16+
'
17+
18+
# environment:
19+
# - CODESPACE=${CODESPACE_NAME}-6060.app.github.dev
20+
# command: /bin/bash -c "if [[ $CODESPACE == -6060* ]]; then CODESPACE_HOST="localhost:6060"; else CODESPACE_HOST=${CODESPACE}; fi && echo "Hallo" && echo ${CODESPACE_HOST} && envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
21+
# environment:
22+
# - CODESPACE_HOST=${CODESPACE_NAME:+${CODESPACE_NAME}-6060.app.github.dev}
23+
# - CODESPACE_HOST=${CODESPACE_HOST:-localhost:6060}
24+
# - CODESPACE_LOCAL=localhost:6060
25+
# - CODESPACE_HOST=${CODESPACE_NAME}-6060.app.github.dev}${CODESPACE_NAME:-localhost:6060}
26+
# - CODESPACE_HOST=${CODESPACE_NAME}-6060.app.github.dev
27+
# command: /bin/bash -c "envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
28+
# command: /bin/bash -c 'export CODESPACE_HOST="${CODESPACE_NAME:+$CODESPACE_NAME-6060.app.github.dev}" && CODESPACE_HOST="${CODESPACE_HOST:-localhost:6060}" && envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf'
29+
# command: /bin/bash -c 'export CODESPACE_HOST=$([ -n "$CODESPACE_NAME" ] && echo "$CODESPACE_NAME-6060.app.github.dev" || echo "localhost:6060"); envsubst '$CODESPACE_HOST' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf'
930

1031
customer-service:
1132
build: customer-service/

0 commit comments

Comments
 (0)