Skip to content

Commit

Permalink
Merge pull request #546 from curveball/developer-tokens-get-client-id
Browse files Browse the repository at this point in the history
Developer tokens enhancements.
  • Loading branch information
evert authored Oct 30, 2024
2 parents 1dc4444 + 7748187 commit 6f3d4a0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
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

0 comments on commit 6f3d4a0

Please sign in to comment.