2015-04-11 07:22:55 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-04-14 01:33:29 +02:00
|
|
|
"time"
|
|
|
|
|
2015-04-11 07:22:55 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-13 10:22:51 +02:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2015-04-11 07:22:55 +02:00
|
|
|
|
2015-04-13 08:15:28 +02:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-11 07:22:55 +02:00
|
|
|
)
|
|
|
|
|
2015-04-13 10:22:51 +02:00
|
|
|
// POST /api/user/tokens
|
2015-04-11 07:22:55 +02:00
|
|
|
func PostToken(c *gin.Context) {
|
2015-04-13 08:15:28 +02:00
|
|
|
store := ToDatastore(c)
|
|
|
|
sess := ToSession(c)
|
|
|
|
user := ToUser(c)
|
2015-04-13 10:22:51 +02:00
|
|
|
|
|
|
|
in := &common.Token{}
|
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
2015-04-13 08:15:28 +02:00
|
|
|
|
|
|
|
token := &common.Token{}
|
2015-04-13 10:22:51 +02:00
|
|
|
token.Label = in.Label
|
|
|
|
token.Repos = in.Repos
|
|
|
|
token.Scopes = in.Scopes
|
2015-04-13 08:15:28 +02:00
|
|
|
token.Login = user.Login
|
|
|
|
token.Kind = common.TokenUser
|
2015-04-14 01:33:29 +02:00
|
|
|
token.Issued = time.Now().UTC().Unix()
|
2015-04-13 08:15:28 +02:00
|
|
|
|
|
|
|
err := store.InsertToken(token)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jwt, err := sess.GenerateToken(token)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
}
|
2015-04-14 01:33:29 +02:00
|
|
|
c.JSON(200, struct {
|
|
|
|
*common.Token
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
}{token, jwt})
|
2015-04-11 07:22:55 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:33:29 +02:00
|
|
|
// DELETE /api/user/tokens/:label
|
2015-04-11 07:22:55 +02:00
|
|
|
func DelToken(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
user := ToUser(c)
|
2015-04-13 07:32:32 +02:00
|
|
|
label := c.Params.ByName("label")
|
2015-04-13 10:22:51 +02:00
|
|
|
|
2015-04-13 07:32:32 +02:00
|
|
|
token, err := store.GetToken(user.Login, label)
|
2015-04-11 07:22:55 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
}
|
|
|
|
err = store.DeleteToken(token)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
}
|
2015-04-13 10:22:51 +02:00
|
|
|
|
|
|
|
c.Writer.WriteHeader(200)
|
2015-04-11 07:22:55 +02:00
|
|
|
}
|