Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer tokens enhancements. #546

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.26.5 (????-??-??)
-------------------

* Developer tokens will now be associated with a client_id if an OAuth2 client
was used to generate one. This allows them to be refreshed.
* A scope can now be specified when creating a developer token.


0.26.4 (2024-10-28)
-------------------

Expand Down
22 changes: 17 additions & 5 deletions src/middleware/login.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Middleware } from '@curveball/core';
import { NotFound, Unauthorized } from '@curveball/http-errors';
import * as oauth2Service from './../oauth2/service.js';
import { App, User, Principal } from '../types.js';
import { App, User, Principal, AppClient } from '../types.js';
import * as privilegeService from '../privilege/service.js';
import * as services from '../services.js';

const whitelistPath = [
'/login',
Expand Down Expand Up @@ -41,8 +42,15 @@ class AuthHelper {
*/
public principal: App | User | null;

constructor(principal: App | User | null) {
/**
* The App Client that was used to authenticate the user. Note that not
* every authentication method uses an app.
*/
public appClient: AppClient | null;

constructor(principal: App | User | null, appClient: AppClient | null) {
this.principal = principal;
this.appClient = appClient;
}

/**
Expand Down Expand Up @@ -102,16 +110,20 @@ export default function(): Middleware {
throw e;
}
}
// We are logged in!
ctx.auth = new AuthHelper(token.principal);

ctx.auth = new AuthHelper(
token.principal,
token.clientId !== 0 ? await services.appClient.findById(token.clientId) : null,
);
ctx.privileges = await privilegeService.get(ctx.auth.principal!);

return next();

}

ctx.auth = new AuthHelper(
ctx.session.user || null
ctx.session.user || null,
null
);
if (ctx.auth.principal) {
ctx.privileges = await privilegeService.get(ctx.auth.principal);
Expand Down
6 changes: 4 additions & 2 deletions src/oauth2/controller/user-access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class UserAccessTokenController extends Controller {

const token = await oauth2Service.generateTokenDeveloperToken({
principal: user,
});

scope: ctx.request.body?.scope?.split(' '),
client: ctx.auth.appClient ?? undefined,
},
);
ctx.response.body = tokenResponse(token);
log(EventType.generateAccessToken, ctx.ip()!, user.id, ctx.request.headers.get('User-Agent'));

Expand Down
8 changes: 5 additions & 3 deletions src/oauth2/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ export async function generateTokenAuthorizationCode(options: GenerateTokenAutho

type GenerateTokenDeveloperTokenOptions = {
principal: User;
client?: AppClient;
scope?: string[];
}
/**
* Generates a token for the 'implicit' GrantType
*/
export function generateTokenDeveloperToken(options: GenerateTokenDeveloperTokenOptions): Promise<OAuth2Token> {
const client: AppClient = {
const client = options.client ?? {
id: 0,
clientId: 'system',
clientSecret: '',
Expand All @@ -244,10 +246,10 @@ export function generateTokenDeveloperToken(options: GenerateTokenDeveloperToken
};
return generateTokenInternal({
grantType: 'developer-token',
...options,
principal: options.principal,
scope: options.scope ?? [],
secretUsed: false,
client,
scope: [],
});
}

Expand Down