106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program 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.
|
|
//
|
|
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package openid
|
|
|
|
import (
|
|
"code.vikunja.io/api/pkg/config"
|
|
"code.vikunja.io/api/pkg/models"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
type Callback struct {
|
|
Code string `query:"code" json:"code"`
|
|
Scope string `query:"scop" json:"scope"`
|
|
State string `query:"state" json:"state"`
|
|
}
|
|
|
|
type Provider struct {
|
|
Name string `json:"name"`
|
|
Key string `json:"key"`
|
|
AuthURL string `json:"auth_url"`
|
|
ClientID string `json:"client_id"`
|
|
}
|
|
|
|
func getKeyFromName(name string) string {
|
|
reg, _ := regexp.Compile("[^a-z0-9]+")
|
|
return reg.ReplaceAllString(strings.ToLower(name), "")
|
|
}
|
|
|
|
func GetAllProviders() (providers []*Provider) {
|
|
rawProvider := config.AuthOpenIDProviders.Get().([]interface{})
|
|
|
|
for _, p := range rawProvider {
|
|
pi := p.(map[interface{}]interface{})
|
|
|
|
providers = append(providers, &Provider{
|
|
Name: pi["name"].(string),
|
|
Key: getKeyFromName(pi["name"].(string)),
|
|
AuthURL: pi["authurl"].(string),
|
|
ClientID: pi["clientid"].(string),
|
|
})
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetProvider(key string) *Provider {
|
|
rawProvider := config.AuthOpenIDProviders.Get().([]interface{})
|
|
|
|
for _, p := range rawProvider {
|
|
pi := p.(map[interface{}]interface{})
|
|
|
|
k := getKeyFromName(pi["name"].(string))
|
|
if k == key {
|
|
return &Provider{
|
|
Name: pi["name"].(string),
|
|
Key: k,
|
|
AuthURL: pi["authurl"].(string),
|
|
ClientID: pi["clientid"].(string),
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func HandleCallback(c echo.Context) error {
|
|
cb := &Callback{}
|
|
if err := c.Bind(cb); err != nil {
|
|
return c.JSON(http.StatusBadRequest, models.Message{Message: "Bad data"})
|
|
}
|
|
|
|
// Check if the provider exists
|
|
providerKey := c.Param("provider")
|
|
provider := GetProvider(providerKey)
|
|
if provider == nil {
|
|
return c.JSON(http.StatusBadRequest, models.Message{Message: "Provider does not exist"})
|
|
}
|
|
|
|
// Parse the access & ID token
|
|
|
|
// Get the userinfo
|
|
|
|
// Check if we have seen this user before
|
|
|
|
// Log them in
|
|
|
|
return nil
|
|
}
|