Add /api/v1/tokens/self/delete

This commit is contained in:
Howl 2016-06-14 10:03:53 +02:00
parent a9b82f99b5
commit 52d7b65493
2 changed files with 15 additions and 0 deletions

View File

@ -24,6 +24,7 @@ func Start(conf common.Conf, dbO *sql.DB) *gin.Engine {
gv1 := api.Group("/v1")
{
gv1.POST("/tokens/new", Method(v1.TokenNewPOST))
gv1.GET("/tokens/self/delete", Method(v1.TokenSelfDeleteGET))
// Auth-free API endpoints
gv1.GET("/ping", Method(v1.PingGET))

View File

@ -123,3 +123,17 @@ func TokenNewPOST(md common.MethodData) common.CodeMessager {
r.Code = 200
return r
}
// TokenSelfDeleteGET deletes the token the user is connecting with.
func TokenSelfDeleteGET(md common.MethodData) common.CodeMessager {
if md.ID() == 0 {
return common.SimpleResponse(400, "How should we delete your token if you haven't even given us one?!")
}
_, err := md.DB.Exec("DELETE FROM tokens WHERE token = ? LIMIT 1",
fmt.Sprintf("%x", md5.Sum([]byte(md.User.Value))))
if err != nil {
md.Err(err)
return Err500
}
return common.SimpleResponse(200, "Bye!")
}