1
1
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-29 10:44:57 +01:00
wg-gen-web/auth/auth.go

48 lines
1.2 KiB
Go
Raw Normal View History

2020-04-28 13:11:49 +02:00
package auth
import (
"fmt"
log "github.com/sirupsen/logrus"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/auth/fake"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/auth/github"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/auth/oauth2oidc"
2020-04-28 13:11:49 +02:00
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
"golang.org/x/oauth2"
"os"
2020-04-28 13:11:49 +02:00
)
type Auth interface {
Setup() error
CodeUrl(state string) string
Exchange(code string) (*oauth2.Token, error)
UserInfo(oauth2Token *oauth2.Token) (*model.User, error)
}
func GetAuthProvider() (Auth, error) {
var oauth2Client Auth
var err error
switch os.Getenv("OAUTH2_PROVIDER_NAME") {
case "fake":
log.Warn("Oauth is set to fake, no actual authentication will be performed")
oauth2Client = &fake.Fake{}
case "oauth2oidc":
log.Warn("Oauth is set to oauth2oidc, must be RFC implementation on server side")
oauth2Client = &oauth2oidc.Oauth2idc{}
case "github":
log.Warn("Oauth is set to github, no openid will be used")
oauth2Client = &github.Github{}
case "google":
return nil, fmt.Errorf("auth provider name %s not yet implemented", os.Getenv("OAUTH2_PROVIDER_NAME"))
default:
return nil, fmt.Errorf("auth provider name %s unknown", os.Getenv("OAUTH2_PROVIDER_NAME"))
}
err = oauth2Client.Setup()
return oauth2Client, err
}