Skip to content

Commit

Permalink
docs(example): add .well-known link in home page
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbrs committed Dec 4, 2022
1 parent 5f86346 commit 50bcba0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 46 deletions.
34 changes: 19 additions & 15 deletions example/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,47 @@ import {
Res,
} from '@nestjs/common';
import { Response } from 'express';
import { Oidc } from 'nest-oidc-provider'
import { Oidc } from 'nest-oidc-provider';
import { KoaContextWithOIDC } from 'oidc-provider';
import axios from 'axios';
import qs from 'query-string';

@Controller()
export class AppController {
private readonly logger = new Logger(AppController.name)
private readonly logger = new Logger(AppController.name);

@Get('/')
@Render('index')
async index(@Oidc.Context() ctx: KoaContextWithOIDC) {
const { oidc: { provider } } = ctx;
const {
oidc: { provider },
} = ctx;
const session = await provider.Session.get(ctx);

const res: Record<string, any> = {
query: ctx.query,
accountId: null,
scopes: null,
origin: ctx.URL.origin,
};

if (session?.accountId) {
const grant = await provider.Grant.find(session.grantIdFor('test'));
return {
query: ctx.query,
accountId: session.accountId,
scopes: grant?.getOIDCScopeEncountered()
}
res.accountId = session.accountId;
res.scopes = grant?.getOIDCScopeEncountered();
}

return {
query: ctx.query,
accountId: null,
scopes: null
}
return res;
}

@Get('/callback')
async test(@Query() query: Record<string, any>, @Res() res: Response) {
const { code, error, error_description } = query;

if (error) {
return res.redirect(`/?error=${error}&error_description=${error_description}`);
return res.redirect(
`/?error=${error}&error_description=${error_description}`,
);
}

if (!code) {
Expand All @@ -69,7 +73,7 @@ export class AppController {
},
);

res.redirect('/')
res.redirect('/');
} catch (err) {
this.logger.error('Could not get token:', err);
res
Expand Down
88 changes: 57 additions & 31 deletions example/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,91 @@
</div>
</div>

<%
if (query.error || query.error_description) { %>
<div class="row">
<div class="col">
<div class="alert alert-danger mx-auto" style="max-width: 500px;">
<p class="m-0"><strong>error:</strong> <%= query.error %></p>
<p class="m-0"><strong>error_description:</strong> <%= query.error_description %></p>
</div>
</div>
<% if (query.error || query.error_description) { %>
<div class="row">
<div class="col">
<div class="alert alert-danger mx-auto" style="max-width: 500px">
<p class="m-0"><strong>error:</strong> <%= query.error %></p>
<p class="m-0">
<strong>error_description:</strong> <%=
query.error_description %>
</p>
</div>
<% } %>
%>
</div>
</div>
<% } %> %>

<div class="row">
<div class="col">
<div class="card mx-auto" style="width: 300px">
<div class="card-body text-center">
<div class="mb-3 pb-2 border-bottom">
<h5 class="card-title mb-0">Welcome<% if (accountId) {%> <strong><%= accountId %></strong> <%} %>!</h5>
<h5 class="card-title mb-0">
Welcome<% if (accountId) {%>
<strong><%= accountId %></strong> <%} %>!
</h5>

<% if (scopes) { %>
<div class="text-start mt-4">
<h6 class="mb-0">Granted scopes:</h6>
<ul>
<% for (const scopeName of scopes.trim().replace(/\s+/, ' ').split(' ')) { %>
<li><%= scopeName %></li>
<% } %>
</ul>
</div>
<% } %>

<% if (accountId) { %>
<a href="http://localhost:3001/oidc/session/end" class="btn btn-warning w-100">Logout</a>
<div class="text-start mt-4">
<h6 class="mb-0">Granted scopes:</h6>
<ul>
<% for (const scopeName of scopes.trim().replace(/\s+/,
' ').split(' ')) { %>
<li><%= scopeName %></li>
<% } %>
</ul>
</div>
<% } %> <% if (accountId) { %>
<a
href="<%= origin %>/oidc/session/end"
class="btn btn-warning w-100"
>Logout</a
>
<% } %>
</div>

<div class="mb-4 text-start">
<label for="scopes">Scopes:</label>
<input class="form-control" type="text" id="scopes" placeholder="openid email" value="openid email">
<input
class="form-control"
type="text"
id="scopes"
placeholder="openid email"
value="openid email"
/>
</div>

<button id="signin" class="btn btn-success w-100">Click here to authenticate</button>
<button id="signin" class="btn btn-success w-100">
Click here to authenticate
</button>
</div>
</div>
</div>
</div>

<div class="row mt-3">
<div class="col text-center">
<a
href="<%= origin %>/oidc/.well-known/openid-configuration"
target="_blank"
>.well-known/openid-configuration</a
>
</div>
</div>
</div>
</div>
</div>

<script>
const btn = document.getElementById('signin')
const btn = document.getElementById('signin');
const scopesInput = document.getElementById('scopes');
const baseUrl = '/oidc/auth?client_id=test&response_type=code&redirect_uri=http://localhost:3001/callback'
const baseUrl =
'/oidc/auth?client_id=test&response_type=code&redirect_uri=<%= origin %>/callback';
btn.onclick = () => {
const scopes = scopesInput.value.replace(/\s+/, ' ')
window.location = `${baseUrl}&scope=${scopes || 'openid email'}`
}
const scopes = scopesInput.value.replace(/\s+/, ' ');
window.location = `${baseUrl}&scope=${scopes || 'openid email'}`;
};
</script>

<script
Expand Down

0 comments on commit 50bcba0

Please sign in to comment.