0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-04 17:31:17 +00:00
wg-gen-web/auth/auth.go
2021-04-06 14:23:23 +02:00

50 lines
1.3 KiB
Go

package auth
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/vx3r/wg-gen-web/auth/fake"
"github.com/vx3r/wg-gen-web/auth/github"
"github.com/vx3r/wg-gen-web/auth/oauth2oidc"
"github.com/vx3r/wg-gen-web/model"
"golang.org/x/oauth2"
"os"
)
// Auth interface to implement as auth provider
type Auth interface {
Setup() error
CodeUrl(state string) string
Exchange(code string) (*oauth2.Token, error)
UserInfo(oauth2Token *oauth2.Token) (*model.User, error)
}
// GetAuthProvider get an instance of auth provider based on config
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
}