![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@iagocalazans/elysia-oauth2
Advanced tools
A plugin for Elysia.js for server-side OAuth 2.0 Authorization Code Flow
A plugin for Elysia.js for server-side OAuth 2.0 Authorization Code Flow
bun add @iagocalazans/elysia-oauth2
import { Elysia } from 'elysia';
import oauth2, { github } from '@iagocalazans/elysia-oauth2';
import { randomBytes } from 'crypto';
const globalState = randomBytes(8).toString('hex');
let globalToken = null;
const app = new Elysia();
const auth = oauth2({
profiles: {
// define multiple OAuth 2.0 profiles
github: {
provider: github(),
scope: ['user']
}
},
state: {
// custom state verification between requests
check(req, name, state) {
return state === globalState;
},
generate(req, name) {
return globalState;
}
},
storage: {
async get ({ cookie }, name) {
return jwt.verify(cookie[`${name}_session`]);
},
async set ({ setCookie, jwt }, name, token) {
setCookie(`${name}_session`, await jwt.sign(token), { maxAge: token.expires_in });
return token;
},
async delete ({ setCookie }, name) {
setCookie(`${name}_session`, '', { maxAge: 0, expires: new Date(Date.now()) });
return {};
}
}
});
function userPage(user: {}, logout: string) {
const html = `<!DOCTYPE html>
<html lang="en">
<body>
User:
<pre>${JSON.stringify(user, null, '\t')}</pre>
<a href="${logout}">Logout</a>
</body>
</html>`;
return new Response(html, { headers: { 'Content-Type': 'text/html' } });
}
app
.use(auth)
.use(cookie({
maxAge: 3600,
secure: true,
httpOnly: true,
sameSite: 'none',
signed: true,
secret: ['Johnny has a cage']
}))
.use(jwt({
name: 'jwt',
secret: 'Lorem Ipsum Dolor Sit Amet'
}))
.get('/', async (ctx) => {
// get login, callback, logout urls for one or more OAuth 2.0 profiles
const profiles = ctx.profiles('github');
// check if one or more OAuth 2.0 profiles are authorized
if (await ctx.authorized('github')) {
const user = await fetch('https://api.github.com/user', {
// ... and use the Authorization header afterwards
headers: await ctx.tokenHeaders('github')
});
return userPage(await user.json(), profiles.github.logout);
}
// Render login page
const html = `<!DOCTYPE html>
<html lang="en">
<body>
<h2>Login with <a href="${profiles.github.login}">Github</a></h2>
</body>
</html>`;
return new Response(html, { headers: { 'Content-Type': 'text/html' } });
})
.listen(3000);
console.log('Listening on http://localhost:3000');
Generate a client id
and client secret
for an OAuth app on Github
Use http://localhost:3000/login/github/authorized
as your Authorization callback URL
Create an .env
file based on the previously generated client credentials:
GITHUB_OAUTH_CLIENT_ID=client id
GITHUB_OAUTH_CLIENT_SECRET=client secret
Bun automatically loads environment variables from .env
files
If you are unsure which URL should be used as Authorization callback URL
call ctx.profiles()
without an argument to get all URLs of all registered OAuth 2.0 Profiles:
app
.use(auth)
.get('/', (ctx) => {
return ctx.profiles();
})
.listen(3000);
import { azure, discord, github, ... } from '@iagocalazans/elysia-oauth2';
All available providers are listed inside the providers folder.
Checkout the examples folder on github for further use cases such as the Bun's sqlite module.
import oauth2, { TOAuth2Provider } from '@iagocalazans/elysia-oauth2';
function myGithub(): TOAuth2Provider {
return {
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
auth: {
url: 'https://github.com/login/oauth/authorize',
params: {
allow_signup: true
}
},
token: {
url: 'https://github.com/login/oauth/access_token',
params: {}
}
};
}
const auth = oauth2({
profiles: {
github: {
provider: myGithub(),
scope: ['user']
}
}
// ...
});
FAQs
A plugin for Elysia.js for server-side OAuth 2.0 Authorization Code Flow
We found that @iagocalazans/elysia-oauth2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.