vikunja-api/pkg/routes/metrics.go
konrad 2b5c9ae7a8 Authentication with OpenID Connect providers (#713)
Add config docs

Lint

Move provider-related stuff to separate file

Refactor getting auth providers

Fix tests

Fix user tests

Fix openid tests

Add swagger docs

Fix lint

Fix lint issues

Fix checking if the user already exists

Make sure to create a new namespace for new users

Docs

Add tests for openid

Remove unnessecary err check

Consistently return nil users if creating a new user failed

Move sending confirmation email to separate function

Better variable names

Move checks to separate functions

Refactor creating user into seperate file

Fix creating new local users

Test creating new users from different issuers

Generate a random username right away if no preferred username has been given

Add todo

Cache openid providers

Add getting int clientids

Fix migration

Move creating tokens to auth package

Add getting or creating a third party user

Add parsing claims

Add retreiving auth tokens

Add token callback from openid package

Add check for provider key

Add routes

Start adding openid auth handler

Add config for openid auth

Co-authored-by: kolaente <k@knt.li>
Reviewed-on: vikunja/api#713
Co-Authored-By: konrad <konrad@kola-entertainments.de>
Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-11-21 16:38:58 +00:00

105 lines
2.5 KiB
Go

// Copyright 2018-2020 Vikunja and contriubtors. All rights reserved.
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package routes
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/models"
auth2 "code.vikunja.io/api/pkg/modules/auth"
"code.vikunja.io/api/pkg/user"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func setupMetrics(a *echo.Group) {
if !config.ServiceEnableMetrics.GetBool() {
return
}
metrics.InitMetrics()
type countable struct {
Rediskey string
Type interface{}
}
for _, c := range []countable{
{
metrics.ListCountKey,
models.List{},
},
{
metrics.UserCountKey,
user.User{},
},
{
metrics.NamespaceCountKey,
models.Namespace{},
},
{
metrics.TaskCountKey,
models.Task{},
},
{
metrics.TeamCountKey,
models.Team{},
},
} {
// Set initial totals
total, err := models.GetTotalCount(c.Type)
if err != nil {
log.Fatalf("Could not get initial count for %v, error was %s", c.Type, err)
}
if err := metrics.SetCount(total, c.Rediskey); err != nil {
log.Fatalf("Could not set initial count for %v, error was %s", c.Type, err)
}
}
a.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
}
func setupMetricsMiddleware(a *echo.Group) {
if !config.ServiceEnableMetrics.GetBool() {
return
}
a.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Update currently active users
if err := updateActiveUsersFromContext(c); err != nil {
log.Error(err)
return next(c)
}
return next(c)
}
})
}
// updateActiveUsersFromContext updates the currently active users in redis
func updateActiveUsersFromContext(c echo.Context) (err error) {
auth, err := auth2.GetAuthFromClaims(c)
if err != nil {
return
}
return metrics.SetUserActive(auth)
}