0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-12-21 00:43:23 +00:00
wg-gen-web/auth/auth.go

50 lines
1.3 KiB
Go
Raw Normal View History

2020-04-28 11:11:49 +00:00
package auth
import (
"fmt"
log "github.com/sirupsen/logrus"
2021-04-06 12:23:23 +00:00
"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"
2020-04-28 11:11:49 +00:00
"golang.org/x/oauth2"
"os"
2020-04-28 11:11:49 +00:00
)
2020-06-10 07:52:44 +00:00
// Auth interface to implement as auth provider
2020-04-28 11:11:49 +00:00
type Auth interface {
Setup() error
CodeUrl(state string) string
Exchange(code string) (*oauth2.Token, error)
UserInfo(oauth2Token *oauth2.Token) (*model.User, error)
}
2020-06-10 07:52:44 +00:00
// 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
}