-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update api endpoints and move backend logic to separate dir to minimi…
…ze vendor lockin
- Loading branch information
Showing
27 changed files
with
471 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const oso = require('../oso'); | ||
|
||
const AuthorizeParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
userId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
action: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
resourceType: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
resourceId: { | ||
$type: 'string', | ||
$required: true | ||
} | ||
}).compile('AuthorizeParams'); | ||
|
||
module.exports = async function authorize(params) { | ||
params = new AuthorizeParams(params); | ||
const authorized = await oso.authorize( | ||
{ type: 'User', id: `${params.sessionId}_${params.userId}` }, | ||
params.action, | ||
{ type: params.resourceType, id: params.resourceId } | ||
); | ||
|
||
return { authorized }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const assert = require('assert'); | ||
const oso = require('../oso'); | ||
|
||
const DeleteFactParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
factType: { | ||
$type: 'string', | ||
$required: true, | ||
$enum: ['role', 'attribute'] | ||
}, | ||
userId: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'role') | ||
}, | ||
role: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'role') | ||
}, | ||
resourceType: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
resourceId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
attribute: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'attribute') | ||
}, | ||
attributeValue: { | ||
$type: 'boolean', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'attribute') | ||
} | ||
}).compile('DeleteFactParams'); | ||
|
||
module.exports = async function deleteFact(params) { | ||
params = new DeleteFactParams(params); | ||
if (params.factType === 'role') { | ||
const resourceId = params.resourceType === 'Repository' ? `${params.sessionId}_${params.resourceId}` : params.resourceId; | ||
await oso.delete( | ||
'has_role', | ||
{ type: 'User', id: `${params.sessionId}_${params.userId}` }, | ||
params.role, | ||
{ type: params.resourceType, id: resourceId } | ||
); | ||
} else { | ||
await oso.delete( | ||
params.attribute, | ||
{ type: 'Repository', id: `${params.sessionId}_${params.resourceId}` }, | ||
{ type: 'Boolean', id: !!params.attributeValue + '' } | ||
); | ||
} | ||
return { ok: true }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const oso = require('../oso'); | ||
|
||
const FactsParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
userId: { | ||
$type: ['string'], | ||
$required: true | ||
} | ||
}).compile('FactsParams'); | ||
|
||
module.exports = async function facts(params) { | ||
params = new FactsParams(params); | ||
const facts = []; | ||
for (const userId of params.userId) { | ||
const factsForUser = await oso.get( | ||
'has_role', | ||
{ type: 'User', id: `${params.sessionId}_${userId}` }, | ||
null, | ||
null | ||
); | ||
facts.push(...factsForUser); | ||
} | ||
for (const repo of ['osohq/sample-apps', 'osohq/nodejs-client', 'osohq/configs']) { | ||
let factsForRepo = await oso.get( | ||
'is_protected', | ||
{ type: 'Repository', id: `${params.sessionId}_${repo}` }, | ||
null, | ||
null | ||
); | ||
facts.push(...factsForRepo); | ||
|
||
factsForRepo = await oso.get( | ||
'is_public', | ||
{ type: 'Repository', id: `${params.sessionId}_${repo}` }, | ||
null, | ||
null | ||
); | ||
facts.push(...factsForRepo); | ||
} | ||
|
||
return { facts }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const Player = require('../db/player'); | ||
const connect = require('../db/connect'); | ||
|
||
module.exports = async function leaderboard() { | ||
await connect(); | ||
|
||
const players = await Player | ||
.find({ levelsCompleted: { $gt: 0 } }) | ||
.select({ email: 0 }) | ||
.sort({ | ||
levelsCompleted: -1, | ||
par: 1, | ||
gameplayTimeMS: 1 | ||
}); | ||
|
||
|
||
return { players }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const Player = require('../db/player'); | ||
const connect = require('../db/connect'); | ||
|
||
const ResumeGameParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
} | ||
}).compile('ResumeGameParams'); | ||
|
||
module.exports = async function resumeGame(params) { | ||
const { sessionId } = new ResumeGameParams(params); | ||
|
||
await connect(); | ||
|
||
const player = await Player.findOne({ | ||
sessionId | ||
}); | ||
|
||
return { player }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const Player = require('../db/player'); | ||
const connect = require('../db/connect'); | ||
const oso = require('../oso'); | ||
|
||
const StartGameParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
name: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
email: { | ||
$type: 'string', | ||
$required: true | ||
} | ||
}).compile('StartGameParams'); | ||
|
||
module.exports = async function handler(params) { | ||
const { sessionId, name, email } = new StartGameParams(params); | ||
|
||
await connect(); | ||
|
||
const player = await Player.create({ | ||
sessionId, | ||
name, | ||
}); | ||
|
||
await oso.tell( | ||
'has_relation', | ||
{ type: 'Repository', id: `${sessionId}_osohq/configs` }, | ||
'organization', | ||
{ type: 'Organization', id: 'osohq' } | ||
); | ||
|
||
await oso.tell( | ||
'has_relation', | ||
{ type: 'Repository', id: `${sessionId}_osohq/sample-apps` }, | ||
'organization', | ||
{ type: 'Organization', id: 'osohq' } | ||
); | ||
|
||
await oso.tell( | ||
'has_relation', | ||
{ type: 'Repository', id: `${sessionId}_osohq/nodejs-client` }, | ||
'organization', | ||
{ type: 'Organization', id: 'osohq' } | ||
); | ||
|
||
return { player }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
const Archetype = require('archetype'); | ||
const assert = require('assert'); | ||
const oso = require('../oso'); | ||
|
||
const TellParams = new Archetype({ | ||
sessionId: { | ||
$type: 'string', | ||
$required: true | ||
}, | ||
factType: { | ||
$type: 'string', | ||
$required: true, | ||
$enum: ['role', 'attribute'] | ||
}, | ||
userId: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'role') | ||
}, | ||
role: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'role') | ||
}, | ||
resourceType: { | ||
$type: 'string', | ||
$required: (doc) => doc.role !== 'superadmin' | ||
}, | ||
resourceId: { | ||
$type: 'string', | ||
$required: (doc) => doc.role !== 'superadmin' | ||
}, | ||
attribute: { | ||
$type: 'string', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'attribute') | ||
}, | ||
attributeValue: { | ||
$type: 'boolean', | ||
$validate: (v, type, doc) => assert.ok(v != null || doc.factType !== 'attribute') | ||
} | ||
}).compile('TellParams'); | ||
|
||
module.exports = async function handler(params) { | ||
const validatedParams = new TellParams(params); | ||
assert.ok( | ||
validatedParams.attribute == null || ['is_public', 'is_protected'].includes(validatedParams.attribute), | ||
'Invalid attribute' | ||
); | ||
|
||
if (validatedParams.factType === 'role') { | ||
const resourceId = validatedParams.resourceType === 'Repository' ? `${validatedParams.sessionId}_${validatedParams.resourceId}` : validatedParams.resourceId; | ||
|
||
if (validatedParams.role === 'superadmin') { | ||
await oso.tell( | ||
'has_role', | ||
{ type: 'User', id: `${validatedParams.sessionId}_${validatedParams.userId}` }, | ||
validatedParams.role | ||
); | ||
} else { | ||
await oso.tell( | ||
'has_role', | ||
{ type: 'User', id: `${validatedParams.sessionId}_${validatedParams.userId}` }, | ||
validatedParams.role, | ||
{ type: validatedParams.resourceType, id: resourceId } | ||
); | ||
} | ||
} else { | ||
await oso.tell( | ||
validatedParams.attribute, | ||
{ type: 'Repository', id: `${validatedParams.sessionId}_${validatedParams.resourceId}` }, | ||
{ type: 'Boolean', id: !!validatedParams.attributeValue + '' } | ||
); | ||
} | ||
|
||
return { ok: true }; | ||
}; |
Oops, something went wrong.