Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl registry token api (#1590)
- Loading branch information
Showing
21 changed files
with
949 additions
and
20 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
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
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,53 @@ | ||
'use strict'; | ||
|
||
var ipRegex = require('ip-regex'); | ||
var tokenService = require('../../../services/token'); | ||
var userService = require('../../../services/user'); | ||
var ipv4 = ipRegex.v4({ exact: true }); | ||
|
||
module.exports = function* createToken() { | ||
var readonly = this.request.body.readonly; | ||
if (typeof readonly !== 'undefined' && typeof readonly !== 'boolean') { | ||
this.status = 400; | ||
var error = '[bad_request] readonly ' + readonly + ' is not boolean'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
var cidrWhitelist = this.request.body.cidr_whitelist; | ||
if (typeof cidrWhitelist !== 'undefined') { | ||
var isValidateWhiteList = Array.isArray(cidrWhitelist) && cidrWhitelist.every(function (cidr) { | ||
return ipv4.test(cidr); | ||
}); | ||
if (!isValidateWhiteList) { | ||
this.status = 400; | ||
var error = '[bad_request] cide white list ' + JSON.stringify(cidrWhitelist) + ' is not validate ip array'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
} | ||
|
||
var password = this.request.body.password; | ||
var user = yield userService.auth(this.user.name, password); | ||
if (!user) { | ||
this.status = 401; | ||
var error = '[unauthorized] incorrect or missing password.'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
|
||
var token = yield tokenService.createToken(this.user.name, { | ||
readonly: !!readonly, | ||
cidrWhitelist: cidrWhitelist || [], | ||
}); | ||
this.status = 201; | ||
this.body = token; | ||
}; |
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,8 @@ | ||
'use strict'; | ||
|
||
var tokenService = require('../../../services/token'); | ||
|
||
module.exports = function* deleteToken() { | ||
yield tokenService.deleteToken(this.user.name, this.params.UUID); | ||
this.status = 204; | ||
}; |
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,60 @@ | ||
'use strict'; | ||
|
||
var tokenService = require('../../../services/token'); | ||
|
||
var DEFAULT_PER_PAGE = 10; | ||
var MIN_PER_PAGE = 1; | ||
var MAX_PER_PAGE = 9999; | ||
|
||
module.exports = function* createToken() { | ||
var perPage = typeof this.query.perPage === 'undefined' ? DEFAULT_PER_PAGE : parseInt(this.query.perPage); | ||
if (Number.isNaN(perPage)) { | ||
this.status = 400; | ||
var error = 'perPage ' + this.query.perPage + ' is not a number'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
if (perPage < MIN_PER_PAGE || perPage > MAX_PER_PAGE) { | ||
this.status = 400; | ||
var error = 'perPage ' + this.query.perPage + ' is out of boundary'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
|
||
var page = typeof this.query.page === 'undefined' ? 0 : parseInt(this.query.page); | ||
if (Number.isNaN(page)) { | ||
this.status = 400; | ||
var error = 'page ' + this.query.page + ' is not a number'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
if (page < 0) { | ||
this.status = 400; | ||
var error = 'page ' + this.query.page + ' is invalidate'; | ||
this.body = { | ||
error, | ||
reason: error, | ||
}; | ||
return; | ||
} | ||
|
||
var tokens = yield tokenService.listToken(this.user.name, { | ||
page: page, | ||
perPage: perPage, | ||
}); | ||
|
||
this.status = 200; | ||
this.body = { | ||
objects: tokens, | ||
urls: {}, | ||
}; | ||
}; |
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
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
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
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
Oops, something went wrong.