add no expriation limited robot account

"-1" means the robot account is a permanent account, no expiration time set.
The ExpiresAt claim is optional, so if it's not set, it will still be considered a valid claim

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-03-08 01:22:12 +08:00
parent e86d3a728c
commit b23111063d
3 changed files with 10 additions and 6 deletions

View File

@ -86,11 +86,15 @@ func (d *DefaultAPIController) CreateRobotAccount(robotReq *model.RobotCreate) (
ProjectID: robotReq.ProjectID,
Access: robotReq.Access,
StandardClaims: jwt.StandardClaims{
IssuedAt: time.Now().UTC().Unix(),
ExpiresAt: robotReq.ExpiresAt,
Issuer: opt.Issuer,
IssuedAt: time.Now().UTC().Unix(),
Issuer: opt.Issuer,
},
}
// "-1" means the robot account is a permanent account, no expiration time set.
// The ExpiresAt claim is optional, so if it's not set, it will still be considered a valid claim
if robot.ExpiresAt != -1 {
rClaims.ExpiresAt = robotReq.ExpiresAt
}
tk, err := token.New(opt, rClaims)
if err != nil {
deferDel = err

View File

@ -79,7 +79,7 @@ func (s *ControllerTestSuite) TestRobotAccount() {
robot2 := &model.RobotCreate{
Name: "robot2",
Description: "TestCreateRobotAccount",
ExpiresAt: expiresAt,
ExpiresAt: -1,
ProjectID: int64(1),
Access: policies,
}

View File

@ -68,8 +68,8 @@ func (rq *RobotCreate) Valid(v *validation.Validation) {
if utils.IsContainIllegalChar(rq.Name, []string{",", "~", "#", "$", "%"}) {
v.SetError("name", "robot name contains illegal characters")
}
if rq.ExpiresAt < 0 {
v.SetError("expires_at", "expiration time must be a positive integer if set")
if rq.ExpiresAt < -1 {
v.SetError("expires_at", "expiration time must be a positive integer or -1 if set")
}
}