3 const crypto
= require('crypto');
4 const StatusError
= require('statuserror');
5 const mongoUtils
= require('gitter-web-persistence-utils/lib/mongo-utils');
6 const oauthService
= require('gitter-web-oauth');
7 const persistenceService
= require('gitter-web-persistence');
8 const restSerializer
= require('../../../serializers/rest-serializer');
13 index
: async
function(req
) {
14 const apps
= await oauthService
.findClientsByOwnerUserId(req
.user
.id
);
16 const strategy
= new restSerializer
.OauthClientStrategy();
17 return restSerializer
.serialize(apps
, strategy
);
20 create
: async
function(req
) {
21 const { name
, registeredRedirectUri
} = req
.body
;
23 if (!name
.match(/\w+/) || !registeredRedirectUri
) {
24 throw new StatusError(400);
27 const safeName
= name
.toLowerCase().replace(/\W/g, '-');
31 registeredRedirectUri
: registeredRedirectUri
,
33 clientKey
: crypto
.randomBytes(20).toString('hex'),
34 clientSecret
: crypto
.randomBytes(20).toString('hex'),
35 ownerUserId
: req
.user
.id
,
36 canSkipAuthorization
: false
39 const oauthClient
= await persistenceService
.OAuthClient
.create(app
);
41 const strategy
= new restSerializer
.OauthClientStrategy();
42 return restSerializer
.serializeObject(oauthClient
, strategy
);
45 destroy
: async
function(req
) {
46 const oauthClient
= req
.oauthClient
;
49 throw new StatusError(404);
52 await oauthService
.deleteOauthClient(oauthClient
);
55 load
: async
function(req
, id
) {
56 if (!mongoUtils
.isLikeObjectId(id
)) throw new StatusError(400);
58 const oauthClient
= await oauthService
.findClientById(id
);
64 if (!mongoUtils
.objectIDsEqual(oauthClient
.ownerUserId
, req
.user
.id
)) {
65 throw new StatusError(403, 'OAuth Client owner does not match your user');