From 52d7b654932cb12b8bb1007696bc93f404a720b4 Mon Sep 17 00:00:00 2001 From: Howl Date: Tue, 14 Jun 2016 10:03:53 +0200 Subject: [PATCH] Add /api/v1/tokens/self/delete --- app/start.go | 1 + app/v1/token.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/start.go b/app/start.go index ddb12d8..de4fe30 100644 --- a/app/start.go +++ b/app/start.go @@ -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)) diff --git a/app/v1/token.go b/app/v1/token.go index 834399c..0fdec5c 100644 --- a/app/v1/token.go +++ b/app/v1/token.go @@ -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!") +}