Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f94d8b3
feat: OIDC example
aritiger Aug 29, 2021
9ba0e56
Merge pull request #1 from shawnhankim/example
shawnhankim Aug 29, 2021
eada718
Merge pull request #2 from shawnhankim/oidc
shawnhankim Aug 29, 2021
772ecdf
feat: access token in NGINX OIDC RP reference implementation
shawndotkim Sep 1, 2021
73c1dcf
feat: refactoring codeExchange and validIdToken, and add validAccessT…
shawndotkim Sep 2, 2021
accefa4
Merge pull request #3 from shawnhankim/access-token
shawnhankim Sep 2, 2021
8bef8d7
Delete example directory
shawnhankim Sep 2, 2021
55c8049
feat: README.MD w/ how to set up and test access token w/ NGINX Plus …
shawndotkim Sep 2, 2021
0da26fb
Merge pull request #4 from shawnhankim/access-token
shawnhankim Sep 2, 2021
fdf11b7
fix: README.md
shawndotkim Sep 2, 2021
744e0c9
fix: README.md
shawndotkim Sep 2, 2021
d0fc7f8
fix: README.md
shawndotkim Sep 2, 2021
15d3d6b
Merge pull request #5 from shawnhankim/access-token
shawnhankim Sep 2, 2021
f6b7519
fix: README.md
shawndotkim Sep 2, 2021
7232493
Merge pull request #6 from shawnhankim/access-token
shawnhankim Sep 2, 2021
93e97cf
fix: gitignore, auxfiles, README.md
shawndotkim Sep 2, 2021
58464d5
Merge pull request #7 from shawnhankim/access-token
shawnhankim Sep 2, 2021
a551eec
fix: reference w/ OpenID Connect Core 1.0 and RFC7519
shawndotkim Sep 2, 2021
bcca61f
Merge pull request #8 from shawnhankim/access-token
shawnhankim Sep 2, 2021
1f6e14e
fix: missing claims
shawndotkim Sep 7, 2021
dbe5a03
Merge pull request #9 from shawnhankim/access-token
shawnhankim Sep 7, 2021
1621d1b
Delete oidc_access_tokens.json
shawnhankim Sep 7, 2021
4fa0267
Delete oidc_id_tokens.json
shawnhankim Sep 7, 2021
f53d50d
Delete refresh_tokens.json
shawnhankim Sep 7, 2021
1a8dbcc
Update mysample.crt
shawnhankim Sep 10, 2021
6e563b3
Update mysample.key
shawnhankim Sep 10, 2021
3170b0a
Delete mysample.crt
shawnhankim Sep 10, 2021
b6b9d74
Delete mysample.key
shawnhankim Sep 10, 2021
512b4c8
fix: remove unnecessary file
shawndotkim Sep 7, 2021
a97e26e
fix: modulization for sample config files and testing for extracting …
shawndotkim Sep 8, 2021
338ee73
feature: bearer access token to the proxied backend service
shawndotkim Sep 9, 2021
fa6cc03
fix: comment for passing bearer access token to proxied backend service
shawndotkim Sep 9, 2021
8f61836
fix: gitignore
shawndotkim Sep 10, 2021
f2bc10c
fix: sample configuration
shawndotkim Sep 10, 2021
6cdc6f3
fix: server block
shawndotkim Sep 10, 2021
b3bd703
fix: README.md
shawndotkim Sep 10, 2021
ad0bbb2
Merge pull request #10 from shawnhankim/access-token
shawnhankim Sep 14, 2021
ff25f5d
feat: PKCE w/ IDPs (Amazon Cognito, OneLogin, Okta) + Refactoring
shawndotkim Sep 11, 2021
d362f24
fix: bearer token w/o cookie
shawndotkim Sep 13, 2021
c8a84ba
feat: pkce w/ onelogin
shawndotkim Sep 13, 2021
8a96635
chore: test amazon cognito PKCE
shawndotkim Sep 14, 2021
5a4496b
chore: pkce w/ onelogin
shawndotkim Sep 14, 2021
ff99bbd
chore: amazon cognito PKCE
shawndotkim Sep 14, 2021
97bd202
chore: README.md for PKCE Setup
shawndotkim Sep 14, 2021
dba8419
fix: remove credentials
shawndotkim Sep 14, 2021
4d915d7
fix: location
shawndotkim Sep 15, 2021
ea31e6b
fix: readme.md
shawndotkim Sep 15, 2021
0cfafe7
chore: how to set up PKCE w/ AWS Cognito
shawndotkim Sep 15, 2021
28ddd38
Update README.md
shawnhankim Sep 15, 2021
fe72d4e
feat: PKCE with multiple IDPs
shawndotkim Sep 15, 2021
90e7f1a
fix: README.md
shawndotkim Sep 15, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: access token in NGINX OIDC RP reference implementation
- refactored auth() - refactored codeExchange()
  • Loading branch information
shawndotkim committed Sep 1, 2021
commit 772ecdf9f9b487cb407e96b8f8b02a664c55e5dd
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.crt
*.key
89 changes: 89 additions & 0 deletions examples/01-access-token/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
FROM debian:buster-slim
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"

# Define NGINX versions for NGINX Plus and NGINX Plus modules
# Uncomment this block and the versioned nginxPackages block in the main RUN
# instruction to install a specific release
ENV NGINX_VERSION 24
ENV NJS_VERSION 0.6.1
ENV PKG_RELEASE 1~buster

# Download certificate and key from the customer portal (https://cs.nginx.com)
# and copy to the build context
COPY nginx-repo.crt /etc/ssl/nginx/
COPY nginx-repo.key /etc/ssl/nginx/

RUN set -x \
# Create nginx user/group first, to be consistent throughout Docker variants
&& addgroup --system --gid 101 nginx \
&& adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y ca-certificates gnupg1 \
&& \
NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
found=''; \
for server in \
ha.pool.sks-keyservers.net \
hkp://keyserver.ubuntu.com:80 \
hkp://p80.pool.sks-keyservers.net:80 \
pgp.mit.edu \
; do \
echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
done; \
test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
# Install the latest release of NGINX Plus and/or NGINX Plus modules
# Uncomment individual modules if necessary
# Use versioned packages over defaults to specify a release
&& nginxPackages=" \
nginx-plus \
# nginx-plus=${NGINX_VERSION}-${PKG_RELEASE} \
# nginx-plus-module-xslt \
# nginx-plus-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} \
# nginx-plus-module-geoip \
# nginx-plus-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} \
# nginx-plus-module-image-filter \
# nginx-plus-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} \
# nginx-plus-module-perl \
# nginx-plus-module-perl=${NGINX_VERSION}-${PKG_RELEASE} \
# nginx-plus-module-njs \
nginx-plus-module-njs=${NGINX_VERSION}+${NJS_VERSION}-${PKG_RELEASE} \
iputils-ping \
vim \
net-tools \
" \
&& echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \
&& printf "deb https://plus-pkgs.nginx.com/debian buster nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
$nginxPackages \
gettext-base \
curl \
&& apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx-plus.list \
&& rm -rf /etc/apt/apt.conf.d/90nginx /etc/ssl/nginx

# Forward request logs to Docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
&& mkdir -p /etc/controller-agent/configurator/auxfiles

RUN rm /etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY content /usr/share/nginx/html
COPY conf /etc/nginx
COPY auxfiles /etc/controller-agent/configurator/auxfiles
VOLUME /usr/share/nginx/html
VOLUME /etc/nginx

RUN chmod -R 777 /etc/nginx
RUN chmod -R 777 /etc/nginx/conf.d

EXPOSE 80
EXPOSE 443
EXPOSE 8090

STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
52 changes: 52 additions & 0 deletions examples/01-access-token/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# NGINX OpenID Connect - Access Token

Reference implementation of NGINX Plus as relying party for OpenID Connect authentication with access token.

## Creating a Docker Image of NGINX Plus
- Create a [Dockerfile](./Dockerfile).
- Download your version of the nginx-repo.crt and nginx-repo.key files via the [customer portal](https://cs.nginx.com/?_ga=2.268586425.912746048.1620625839-85838359.1596947109).

## Create Docker Network
- Create a user-defined bridge network:
```bash
$ docker network create my-net
```

## Start IDP such as Keycloak
- From a terminal start Keycloak with the following command:
```bash
$ docker run --name my-idp --network my-net \
-p 8080:8080 \
-e KEYCLOAK_USER=admin \
-e KEYCLOAK_PASSWORD=admin \
-d jboss/keycloak
```

- Execute the following command if you want to stop and remove the container:
```bash
$ docker stop my-idp; docker rm my-idp
```

## Creating Docker Image for NGINX Plus w/ OIDC
- Create a Docker image called `nginxoidc`:
```bash
$ docker build --no-cache -t nginxoidc .
```

- Create a container named my-apigw based on this image:
```bash
$ docker run --name my-apigw --network my-net \
-p 443:443 -p 8090:8090 \
--link my-idp:my-idp \
-d nginxoidc
```

- Execute the following command if you want to stop and remove the container:
```bash
$ docker stop my-apigw; docker rm my-apigw
```

## Reference
- [NGINX OpenID Connect](https://github.com/shawnhankim/nginx-openid-connect)
- [Keycloak on Docker](https://www.keycloak.org/getting-started/getting-started-docker)
- [Enabling Single Sign-On for Proxied Applications](https://docs.nginx.com/nginx/deployment-guides/single-sign-on/)
42 changes: 42 additions & 0 deletions examples/01-access-token/conf/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
server {
listen 9090;
server_name localhost;

location /v1/api/one {
default_type application/json;
return 200 '{"code": "1", "message": "This is for testing /v1/api/one"}';
}
location /v1/api/two {
default_type application/json;
return 200 '{"code": "2", "message": "This is for testing /v1/api/two"}';
}
}

server {
listen 80;
listen [::]:80;
server_name localhost;
status_zone localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

location /v1/api/one {
status_zone localhost_api_one;
proxy_pass http://localhost:9090/v1/api/one;
}

location /v1/api/two {
status_zone localhost_api_two;
proxy_pass http://localhost:9090/v1/api/two;
}

# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

81 changes: 81 additions & 0 deletions examples/01-access-token/conf/conf.d/frontend.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# This is the backend application we are protecting with OpenID Connect
upstream my_backend {
zone my_backend 64k;
server 10.0.0.1:80;
}

# Custom log format to include the 'sub' claim in the REMOTE_USER field
log_format main_jwt '$remote_addr - $jwt_claim_sub [$time_local] "$request" $status '
'$body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

# The frontend server - reverse proxy with OpenID Connect authentication
#
server {
include conf.d/oidc_server.conf; # Authorization code flow and Relying Party processing
error_log /var/log/nginx/error.log debug; # Reduce severity level as required
access_log /var/log/nginx/access.log main;

server_name mynginxoidc.keycloak;
listen 443 ssl;

ssl_certificate /etc/controller-agent/configurator/auxfiles/mysample.crt;
ssl_certificate_key /etc/controller-agent/configurator/auxfiles/mysample.key;
ssl_session_cache off;
ssl_prefer_server_ciphers off;

location / {
# This site is protected with OpenID Connect
auth_jwt "" token=$session_jwt;
error_page 401 = @do_oidc_flow;

#auth_jwt_key_file $oidc_jwt_keyfile; # Enable when using filename
auth_jwt_key_request /_jwks_uri; # Enable when using URL

# Successfully authenticated users are proxied to the backend,
# with 'sub' claim passed as HTTP header
proxy_set_header username $jwt_claim_sub;
# proxy_pass http://my_backend; # The backend site/app
root /usr/share/nginx/html;
index index.html index.htm;

access_log /var/log/nginx/access.log main_jwt;
}
}


server {
include conf.d/oidc_server.conf; # Authorization code flow and Relying Party processing
error_log /var/log/nginx/error.log debug; # Reduce severity level as required
access_log /var/log/nginx/access.log main;

server_name mynginxoidc.aws;
listen 443 ssl; # Use SSL/TLS in production
ssl_certificate /etc/controller-agent/configurator/auxfiles/mysample.crt;
ssl_certificate_key /etc/controller-agent/configurator/auxfiles/mysample.key;
ssl_session_cache off;
ssl_prefer_server_ciphers off;

location / {
# This site is protected with OpenID Connect
auth_jwt "" token=$session_jwt;
error_page 401 = @do_oidc_flow;

#auth_jwt_key_file $oidc_jwt_keyfile; # Enable when using filename
auth_jwt_key_request /_jwks_uri; # Enable when using URL

# Successfully authenticated users are proxied to the backend,
# with 'sub' claim passed as HTTP header
proxy_set_header username $jwt_claim_sub;
# proxy_pass http://my_backend; # The backend site/app
root /usr/share/nginx/html;
index index.html index.htm;

add_header X-Sub $jwt_claim_sub;
add_header X-Email $jwt_claim_email;
add_header X-ID-Token $session_jwt;
add_header X-Access-Token $access_token;
access_log /var/log/nginx/access.log main_jwt;
}
}

# vim: syntax=nginx
Loading