func init() { var tokenFormValidator = v.NewFormValidator() tokenFormValidator.IntField("alert_on").Min(0) app.Api.Get("/api_tokens/", lib.Admin.Required( func(res *wcg.Response, req *wcg.Request) { d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req)) tokens := make([]*models.ApiToken, 0) _, err := d.NewQuery().GetAll(&tokens) if err != nil { panic(err) } res.WriteJson(tokens) }, )) app.Api.Post("/api_tokens/", lib.Admin.Required( func(res *wcg.Response, req *wcg.Request) { d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req)) t, err := d.Issue(req.Form("desc")) if err != nil { app.Api.InternalError(res, req, err) } app.Api.Created(res, req, t.Token) }, )) put_fn := lib.Admin.Required( func(res *wcg.Response, req *wcg.Request) { err := tokenFormValidator.Eval(req.HttpRequest().PostForm) if err != nil { app.Api.BadRequest(res, req, err) return } alertOn, _ := strconv.Atoi(req.Form("alert_on")) d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req)) err = d.Update(req.Param("token"), req.Form("desc"), time.Duration(alertOn)) if err != nil { app.Api.InternalError(res, req, err) } app.Api.Ok(res, req) }, ) app.Api.Put("/api_tokens/:token/", put_fn) // TODO: remove this after client fixed. app.Api.Put("/api_tokens/:token.json", put_fn) delete_fn := lib.Admin.Required( func(res *wcg.Response, req *wcg.Request) { d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req)) err := d.Revoke(req.Param("token")) if err != nil { app.Api.InternalError(res, req, err) } app.Api.Ok(res, req) }, ) app.Api.Delete("/api_tokens/:token/", delete_fn) // TODO: remove this after client fixed. app.Api.Delete("/api_tokens/:token.json", delete_fn) }
func TestUpdateApiToken(t *testing.T) { test.RunTestServer(func(ts *test.TestServer) { assert := test.NewAssert(t) appCtx := apptest.NewAppContextFromTestServer(app, ts) // prepare d := lib.NewApiTokenDriver(appCtx) tok, _ := d.Issue("token1") d.Issue("token2") util.WaitFor(func() bool { c, _ := d.NewQuery().Count() return c == 2 }, util.DefaultWaitForTimeout) var tokUrl = fmt.Sprintf("/api/admin/api_tokens/%s/", tok.Key()) // Case 400 req := ts.PutForm(tokUrl, url.Values{ "desc": []string{"updated"}, "alert_on": []string{"-20"}, }) lib.SetApiTokenForTest(req, lib.Admin) res := req.RouteTo(app.Routes()) assert.HttpStatus(400, res) // Case 200 req = ts.PutForm(tokUrl, url.Values{ "desc": []string{"updated"}, "alert_on": []string{"30"}, }) lib.SetApiTokenForTest(req, lib.Admin) res = req.RouteTo(app.Routes()) assert.HttpStatus(200, res) }) }
func init() { app.Cron.Get( "Monitor API tokens", "every 1 minutes", "/api_tokens/", lib.Admin.Required( func(res *wcg.Response, req *wcg.Request) { var tokens []*models.ApiToken d := lib.NewApiTokenDriver(lib.NewAppContextFromRequest(req)) _, err := d.NewQuery().GetAll(&tokens) if err != nil { panic(err) } m := 0 c := 0 for _, t := range tokens { if t.AlertOn > 0 { m += 1 if t.ShouldAlert() { c += 1 req.Logger.Fatal( "API Token '%s' losts access. The last access time was %s (Threshould: %d minutes).", t.Token, t.LastAccess, t.AlertOn, ) } } } if c > 0 { notifications.SendAlert( req, "API token(s) have not been accessed for a while.", fmt.Sprintf(TMPL_ALERT_BODY, c, wcg.AbsoluteUrl(req, "/admin/clients.html")), ) } res.WriteJson(map[string]interface{}{ "alerted_tokens": c, "monitored_tokens": m, "total_tokens": len(tokens), }) }, )) }
func TestIssueApiToken(t *testing.T) { test.RunTestServer(func(ts *test.TestServer) { assert := test.NewAssert(t) appCtx := apptest.NewAppContextFromTestServer(app, ts) req := ts.PostForm("/api/admin/api_tokens/", url.Values{ "desc": []string{"new token"}, }) lib.SetApiTokenForTest(req, lib.Admin) res := req.RouteTo(app.Routes()) assert.HttpStatus(201, res) var got []*models.ApiToken d := lib.NewApiTokenDriver(appCtx) util.WaitFor(func() bool { d.NewQuery().GetAll(&got) return got != nil && len(got) == 1 }, util.DefaultWaitForTimeout) assert.EqStr("new token", got[0].Description, "IssueApiToken Description") }) }
func TestCronApiTokens(t *testing.T) { test.RunTestServer(func(ts *test.TestServer) { assert := test.NewAssert(t) appCtx := apptest.NewAppContextFromTestServer(app, ts) // prepare d := lib.NewApiTokenDriver(appCtx) tok1, _ := d.Issue("token1") tok2, _ := d.Issue("token2") d.Issue("token3") // not monitored util.WaitFor(func() bool { c, _ := d.NewQuery().Count() return c == 3 }, util.DefaultWaitForTimeout) now := time.Now() tok1.LastAccess = now tok1.AlertOn = 1 * time.Hour key := d.NewKey(tok1.Key(), 0, nil) d.SyncPut(key, tok1) tok2.LastAccess = now.Add(-1 * 60 * 60 * time.Second) // 1 hour before tok2.AlertOn = 30 * time.Second key = d.NewKey(tok2.Key(), 0, nil) d.SyncPut(key, tok2) // test var got map[string]int req := ts.Get("/cron/admin/api_tokens/") lib.SetApiTokenForTest(req, lib.Admin) res := req.RouteTo(app.Routes()) res.Json(&got) assert.EqInt(1, got["alerted_tokens"], "alerted_tokens") assert.EqInt(2, got["monitored_tokens"], "monitored_tokens") assert.EqInt(3, got["total_tokens"], "total_tokens") }) }
func TestRevokeApiToken(t *testing.T) { test.RunTestServer(func(ts *test.TestServer) { assert := test.NewAssert(t) appCtx := apptest.NewAppContextFromTestServer(app, ts) // prepare d := lib.NewApiTokenDriver(appCtx) tok, _ := d.Issue("token1") d.Issue("token2") util.WaitFor(func() bool { c, _ := d.NewQuery().Count() return c == 2 }, util.DefaultWaitForTimeout) var tokUrl = fmt.Sprintf("/api/admin/api_tokens/%s/", tok.Key()) // Case 200 req := ts.Delete(tokUrl) lib.SetApiTokenForTest(req, lib.Admin) res := req.RouteTo(app.Routes()) assert.HttpStatus(200, res) }) }
func TestListApiTokens(t *testing.T) { test.RunTestServer(func(ts *test.TestServer) { assert := test.NewAssert(t) appCtx := apptest.NewAppContextFromTestServer(app, ts) // prepare d := lib.NewApiTokenDriver(appCtx) d.Issue("token1") d.Issue("token2") util.WaitFor(func() bool { c, _ := d.NewQuery().Count() return c == 2 }, util.DefaultWaitForTimeout) // test var got []*models.ApiToken req := ts.Get("/api/admin/api_tokens/") lib.SetApiTokenForTest(req, lib.Admin) res := req.RouteTo(app.Routes()) assert.HttpStatus(200, res) res.Json(&got) assert.EqInt(2, len(got), "ListApiToken length") }) }