Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Support API key metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jul 18, 2019
1 parent b80c6e8 commit cc41b1c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.0.76",
"version": "1.0.77",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
6 changes: 4 additions & 2 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Target Server Version : 100221
File Encoding : 65001
Date: 18/07/2019 10:08:21
Date: 18/07/2019 11:18:22
*/

SET NAMES utf8mb4;
Expand All @@ -22,7 +22,9 @@ SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
DROP TABLE IF EXISTS `staart-api-keys`;
CREATE TABLE `staart-api-keys` (
`id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
`description` text COLLATE utf8mb4_bin DEFAULT NULL,
`jwtApiKey` text COLLATE utf8mb4_bin NOT NULL,
`organizationId` int(12) NOT NULL,
`ipRestrictions` text COLLATE utf8mb4_bin DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion setup/internal/staart-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.76
1.0.77
40 changes: 24 additions & 16 deletions src/controllers/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,24 @@ export class OrganizationController {
}

@Put(":id/api-keys")
@Middleware(
validator(
{
scopes: Joi.string(),
ipRestrictions: Joi.string(),
referrerRestrictions: Joi.string(),
name: Joi.string(),
description: Joi.string()
},
"body"
)
)
async putUserApiKeys(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
joiValidate(
{
scopes: Joi.string().allow(null),
ipRestrictions: Joi.string().allow(null),
referrerRestrictions: Joi.string().allow(null)
},
req.body
);
res
.status(CREATED)
.json(
Expand Down Expand Up @@ -577,6 +581,18 @@ export class OrganizationController {
}

@Patch(":id/api-keys/:apiKeyId")
@Middleware(
validator(
{
scopes: Joi.string().allow(""),
ipRestrictions: Joi.string().allow(""),
referrerRestrictions: Joi.string().allow(""),
name: Joi.string().allow(""),
description: Joi.string().allow("")
},
"body"
)
)
async patchUserApiKey(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const apiKeyId = req.params.apiKeyId;
Expand All @@ -587,14 +603,6 @@ export class OrganizationController {
},
{ id, apiKeyId }
);
joiValidate(
{
scopes: Joi.string().allow(null),
ipRestrictions: Joi.string().allow(null),
referrerRestrictions: Joi.string().allow(null)
},
req.body
);
res.json(
await updateApiKeyForUser(
localsToTokenOrKey(res),
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export const apiKeyToken = (apiKey: ApiKey) => {
const createApiKey = { ...apiKey };
delete createApiKey.createdAt;
delete createApiKey.updatedAt;
delete createApiKey.name;
delete createApiKey.description;
delete createApiKey.expiresAt;
return generateToken(
createApiKey,
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/tables/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface Organization {

export interface ApiKey {
id?: number;
name?: string;
description?: string;
jwtApiKey?: string;
scopes?: string;
organizationId: number;
Expand Down

0 comments on commit cc41b1c

Please sign in to comment.