Skip to content

Commit

Permalink
fix(arango): update arangojs and token CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Aug 2, 2024
1 parent d11f03a commit 5347ea2
Show file tree
Hide file tree
Showing 35 changed files with 427 additions and 428 deletions.
1 change: 1 addition & 0 deletions common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
INSPECT: ${INSPECT---inspect=0.0.0.0:9229}
DEBUG: ${DEBUG-*}
PINO_LEVEL: ${PINO_LEVEL-debug}
PROM_HOST: '::'
DEBUG_HIDE_DATE: ~
DEBUG_COLORS: ~
DEBUG_DEPTH: ~
Expand Down
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
// @ts-check

export * from './oada/eslint.config.mjs';

4 changes: 2 additions & 2 deletions oada/libs/lib-arangodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@oada/models": "workspace:^",
"@oada/oadaify": "^2.1.0",
"@oada/types": "^4.0.0",
"arangojs": "^8.8.1",
"arangojs": "^9.0.0",
"bcryptjs": "^2.4.3",
"debug": "^4.3.6",
"deep-equal": "^2.2.3",
Expand All @@ -75,7 +75,7 @@
"@types/deep-equal": "^1.0.4",
"@types/flat": "^5.0.5",
"@types/json-pointer": "^1.0.34",
"@types/node": "^22.0.0",
"@types/node": "^22.0.2",
"ava": "6.1.3",
"type-fest": "^4.23.0"
},
Expand Down
6 changes: 3 additions & 3 deletions oada/libs/lib-arangodb/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import libConfig from '@oada/lib-config';

import type { CreateCollectionOptions } from 'arangojs/collection.js';
import type { EdgeDefinitionOptions } from 'arangojs/graph.js';
import type { CreateCollectionOptions } from 'arangojs/collection';
import type { EdgeDefinitionOptions } from 'arangojs/graph';

export interface Collection {
name: string;
Expand Down Expand Up @@ -165,7 +165,7 @@ export const { config, schema } = await libConfig({
default: {
name: 'deviceCodes',
indexes: ['deviceCode', 'userCode'],
defaults: './libs/exampledocs/codes.js',
// Defaults: './libs/exampledocs/deviceCodes.js',
},
},
resources: {
Expand Down
4 changes: 2 additions & 2 deletions oada/libs/lib-arangodb/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { config } from './config.js';

import { ArangoError, ArangoErrorCode } from './libs/errors.js';

import type { AqlQuery } from 'arangojs/aql.js';
import type { AqlQuery } from 'arangojs/aql';
import { Database } from 'arangojs';
import type { QueryOptions } from 'arangojs/database.js';
import type { QueryOptions } from 'arangojs/database';
import debug from 'debug';

const error = debug('arangodb#aql:error');
Expand Down
9 changes: 6 additions & 3 deletions oada/libs/lib-arangodb/src/libs/deviceCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ export async function findByUserCode(
export async function save(
deviceCode: DeviceCode,
): Promise<DeviceCode | undefined> {
const { new: saved } = await deviceCodes.save(deviceCode, { returnNew: true });
const { new: saved } = await deviceCodes.save(deviceCode, {
returnNew: true,
});
return saved;
}

export async function redeem(deviceCode: string) {
const cursor = await database.query<{ redeemed: boolean, code?: DeviceCode }>(
const cursor = await database.query<{ redeemed: boolean; code?: DeviceCode }>(
aql`
LET code = (
FOR c IN ${deviceCodes}
Expand All @@ -83,6 +85,7 @@ export async function redeem(deviceCode: string) {
redeemed: LENGTH(clear) == 1,
code: FIRST(code),
}
`);
`,
);
return (await cursor.next())!;
}
2 changes: 1 addition & 1 deletion oada/libs/lib-arangodb/src/libs/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

// TODO: Add more of the error codes from above link

export { ArangoError } from 'arangojs/error.js';
export { ArangoError } from 'arangojs/error';

/**
* Error codes for ArangoErrorCode
Expand Down
7 changes: 1 addition & 6 deletions oada/libs/lib-arangodb/src/libs/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
* limitations under the License.
*/

import type { CollectionReadOptions } from 'arangojs/collection.js';
import type { CollectionReadOptions } from 'arangojs/collection';
import { aql } from 'arangojs';
import bcrypt from 'bcryptjs';
import debug from 'debug';
import { flatten } from 'flat';

import type { Selector } from '../util.js';
import { config } from '../config.js';
Expand Down Expand Up @@ -167,10 +166,6 @@ export async function update(
return sanitizeResult(u.new);
}

export async function like(u: Partial<User>): Promise<AsyncIterable<User>> {
return users.byExample(flatten(u));
}

export async function hashPw(pw: string): Promise<string> {
return bcrypt.hash(pw, roundsOrSalt);
}
1 change: 0 additions & 1 deletion oada/libs/lib-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export default async function loadConfig<S>(
// Allow extra items
allowed: 'warn',
// Do not actually output warnings about extra items?

output() {},
});

Expand Down
2 changes: 1 addition & 1 deletion oada/libs/lib-kafka/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@ava/typescript": "^5.0.0",
"@types/convict": "^6.1.6",
"@types/debug": "^4.1.12",
"@types/node": "^22.0.0",
"@types/node": "^22.0.2",
"@types/uuid": "^10.0.0",
"ava": "6.1.3"
},
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/lib-prom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"devDependencies": {
"@ava/typescript": "^5.0.0",
"@types/convict": "^6.1.6",
"@types/node": "^22.0.0",
"@types/node": "^22.0.2",
"@types/ws": "^8.5.12",
"ava": "6.1.3",
"fastify-plugin": "^4.5.1"
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"node": "22.5.1"
},
"devDependencies": {
"@types/node": "^22.0.0",
"@types/node": "^22.0.2",
"jose": "^5.6.3"
}
}
2 changes: 1 addition & 1 deletion oada/libs/pino-debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/node": "^22.0.0"
"@types/node": "^22.0.2"
},
"peerDependencies": {
"debug": "*"
Expand Down
3 changes: 3 additions & 0 deletions oada/libs/pino-debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ function createRootLogger(): Logger {
'access_token',
'*.access_token',
'*.*.access_token',
'jti',
'*.jti',
'*.*.jti',
'client_secret',
'client.client_secret',
'*.client.client_secret',
Expand Down
2 changes: 1 addition & 1 deletion oada/oada.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
collections: {
users: {
name: 'users',
indexes: ['username', { name: 'oadaid', sparse: true }],
indexes: ['username', { name: 'oadaid', sparse: true }, 'sub', 'iss'],
defaults: './libs/exampledocs/users.js',
},
clients: {
Expand Down
8 changes: 4 additions & 4 deletions oada/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"@tsconfig/node20": "^20.1.4",
"@types/eslint": "^9.6.0",
"@types/mocha": "^10.0.7",
"@types/node": "^22.0.0",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@types/node": "^22.0.2",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@yarnpkg/sdks": "^3.1.3",
"browserslist": "^4.23.2",
"c8": "^10.1.2",
Expand Down Expand Up @@ -59,7 +59,7 @@
"prettier": "^3.3.3",
"tslib": "2.6.3",
"typescript": "5.5.4",
"typescript-eslint": "^7.18.0",
"typescript-eslint": "^8.0.0",
"update-browserslist-db": "^1.1.0",
"zx": "^8.1.4"
},
Expand Down
4 changes: 2 additions & 2 deletions oada/services/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@oada/types": "^4.0.0",
"@oada/well-known-json": "^4.0.2",
"@qlever-llc/interface2class": "^1.1.0",
"arangojs": "^8.8.1",
"arangojs": "^9.0.0",
"bcryptjs": "^2.4.3",
"chalk": "^5.3.0",
"cmd-ts": "^0.13.0",
Expand All @@ -99,7 +99,7 @@
"passport-oauth2-client-password": "~0.1.2",
"pem-jwk": "^2.0.0",
"qrcode-terminal": "^0.12.0",
"qs": "^6.12.3",
"qs": "^6.13.0",
"random-id-base36": "^1.0.3",
"serialize-error": "^11.0.3",
"tslib": "2.6.3",
Expand Down
2 changes: 1 addition & 1 deletion oada/services/auth/src/cli/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

/* eslint-disable no-console */
/* eslint-disable no-console -- This is a CLI */

import '@oada/pino-debug';

Expand Down
27 changes: 18 additions & 9 deletions oada/services/auth/src/cli/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

/* eslint-disable no-console */
/* eslint-disable no-console -- This is a CLI */

import '@oada/pino-debug';

Expand Down Expand Up @@ -147,10 +147,13 @@ const UserType: Type<string, User> = {
},
};

/**
* @todo support other token storage??
*/
export const create = command({
name: 'create',
aliases: ['issue', 'new'],
description: 'Create a token (only works when using local auth)',
description: 'Create a token (only works when using local auth in arango)',
args: {
iss,
scope,
Expand All @@ -172,24 +175,30 @@ export const create = command({
long: 'exp',
short: 'e',
}),
jti: positional({
type: optional(string),
displayName: 'token id',
description: 'Token to create',
}),
},
// eslint-disable-next-line @typescript-eslint/no-shadow
async handler({ iss, scope, user: { sub }, iat, exp }) {
async handler({ iss, scope, user: { sub }, iat, exp, jti }) {
const { Authorization } = await import('@oada/models/authorization');
const { getToken } = await import('../oauth2.js');
const { create: createToken } = await import('../db/arango/tokens.js');

const auth = new Authorization({
iss: iss ? `${iss}` : `${config.get('oidc.issuer')}`,
sub,
iat: iat as number,
exp: exp as number,
scope: scope?.join(' '),
jti,
});
const token = await getToken(
iss ? `${iss}` : `${config.get('oidc.issuer')}`,
auth,
);
const token = await createToken(auth);

console.log(token);
if (!jti) {
console.log(token);
}
},
});

Expand Down
2 changes: 1 addition & 1 deletion oada/services/auth/src/db/arango/deviceCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export const redeem = async function (deviceCode) {
return {
redeemed,
code: code as DeviceCode,
}
};
} satisfies IDeviceCodes['redeem'];
7 changes: 6 additions & 1 deletion oada/services/auth/src/db/arango/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import debug from 'debug';

import type { User } from '@oada/models/user';
import { config } from '../../config.js';
import { findByUsername } from './users.js';

const log = debug('arango/init');

Expand Down Expand Up @@ -105,7 +106,11 @@ async function init() {
const users = systemDatabase.collection('users');
for await (const user of Object.values(defaultUsers)) {
try {
await users.firstExample({ username: user.username });
const u = await findByUsername(user.username);
if (!u) {
throw new Error(`User ${user.username} not found`);
}

log('User % s exists', user.username);
} catch {
log(user, 'Saving user');
Expand Down
13 changes: 10 additions & 3 deletions oada/services/auth/src/db/models/deviceCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export interface IDeviceCodes extends Store {
userCode: DeviceCode['userCode'],
): Promisable<Partial<DeviceCode> | undefined>;
save<C extends DeviceCode>(code: C): Promisable<C>;
redeem(code: DeviceCode['deviceCode']): Promisable<{ redeemed: boolean, code?: DeviceCode }>;
redeem(
code: DeviceCode['deviceCode'],
): Promisable<{ redeemed: boolean; code?: DeviceCode }>;
}

const dataStores = await getDataStores<IDeviceCodes>(
Expand Down Expand Up @@ -117,7 +119,10 @@ export async function activate(
}
}

export async function redeem(clientId: string, deviceCode: DeviceCode['deviceCode']): Promise<{ redeemed: boolean, code?: DeviceCode }> {
export async function redeem(
clientId: string,
deviceCode: DeviceCode['deviceCode'],
): Promise<{ redeemed: boolean; code?: DeviceCode }> {
async function redeemDeviceCode(dataStore: IDeviceCodes) {
const { redeemed, code } = await dataStore.redeem(deviceCode);
if (!code) {
Expand All @@ -135,5 +140,7 @@ export async function redeem(clientId: string, deviceCode: DeviceCode['deviceCod
};
}

return (await tryDataStores(dataStores, redeemDeviceCode)) ?? { redeemed: false }
return (
(await tryDataStores(dataStores, redeemDeviceCode)) ?? { redeemed: false }
);
}
27 changes: 13 additions & 14 deletions oada/services/auth/src/db/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export async function getDataStores<T extends Store>(
item: string,
): Promise<T[]> {
const array = Array.isArray(stores) ? stores : [stores];
const promises = array.map(
async (dataStore) => {
const store: unknown = await import(path.join(dirname, '..', dataStore, `${item}.js`));
return {
name: dataStore,
// @ts-expect-error stuff
...store
} as T
}
);
const promises = array.map(async (dataStore) => {
const store: unknown = await import(
path.join(dirname, '..', dataStore, `${item}.js`)
);
return {
name: dataStore,
// @ts-expect-error stuff
...store,
} as T;
});

return Promise.all(promises);
}
Expand Down Expand Up @@ -67,8 +67,7 @@ export async function tryDataStores<T extends Store, R>(
}

const names = stores.map(({ name }) => name);
throw new Error(
`${queryFun.name} failed to find result(s) among ${names}`,
{ cause: errors },
);
throw new Error(`${queryFun.name} failed to find result(s) among ${names}`, {
cause: errors,
});
}
Loading

0 comments on commit 5347ea2

Please sign in to comment.