2015-04-11 05:22:55 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-04-13 23:33:29 +00:00
|
|
|
"time"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin/binding"
|
2015-04-11 05:22:55 +00:00
|
|
|
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-04-11 05:22:55 +00:00
|
|
|
)
|
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
// POST /api/user/tokens
|
2015-04-11 05:22:55 +00:00
|
|
|
func PostToken(c *gin.Context) {
|
2015-05-18 18:42:00 +00:00
|
|
|
sess := ToSession(c)
|
2015-04-13 06:15:28 +00:00
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
2015-04-13 08:22:51 +00:00
|
|
|
|
|
|
|
in := &common.Token{}
|
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
2015-04-13 06:15:28 +00:00
|
|
|
|
|
|
|
token := &common.Token{}
|
2015-04-13 08:22:51 +00:00
|
|
|
token.Label = in.Label
|
2015-05-11 07:45:31 +00:00
|
|
|
token.UserID = user.ID
|
|
|
|
// token.Repos = in.Repos
|
|
|
|
// token.Scopes = in.Scopes
|
2015-04-13 06:15:28 +00:00
|
|
|
token.Login = user.Login
|
|
|
|
token.Kind = common.TokenUser
|
2015-04-13 23:33:29 +00:00
|
|
|
token.Issued = time.Now().UTC().Unix()
|
2015-04-13 06:15:28 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
err := store.AddToken(token)
|
2015-04-13 06:15:28 +00:00
|
|
|
if err != nil {
|
2015-05-18 17:49:26 +00:00
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-13 06:15:28 +00:00
|
|
|
jwt, err := sess.GenerateToken(token)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
2015-05-18 17:49:26 +00:00
|
|
|
return
|
2015-04-13 06:15:28 +00:00
|
|
|
}
|
2015-05-18 17:49:26 +00:00
|
|
|
|
2015-04-13 23:33:29 +00:00
|
|
|
c.JSON(200, struct {
|
|
|
|
*common.Token
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
}{token, jwt})
|
2015-04-11 05:22:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 23:33:29 +00:00
|
|
|
// DELETE /api/user/tokens/:label
|
2015-04-11 05:22:55 +00:00
|
|
|
func DelToken(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
2015-04-13 05:32:32 +00:00
|
|
|
label := c.Params.ByName("label")
|
2015-04-13 08:22:51 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
token, err := store.TokenLabel(user, label)
|
2015-04-11 05:22:55 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
2015-05-18 17:49:26 +00:00
|
|
|
return
|
2015-04-11 05:22:55 +00:00
|
|
|
}
|
2015-04-15 05:04:38 +00:00
|
|
|
err = store.DelToken(token)
|
2015-04-11 05:22:55 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
2015-05-18 17:49:26 +00:00
|
|
|
return
|
2015-04-11 05:22:55 +00:00
|
|
|
}
|
2015-04-13 08:22:51 +00:00
|
|
|
|
|
|
|
c.Writer.WriteHeader(200)
|
2015-04-11 05:22:55 +00:00
|
|
|
}
|