2020-04-28 11:11:49 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-04-30 09:14:46 +00:00
|
|
|
"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 11:11:49 +00:00
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
|
|
|
|
"golang.org/x/oauth2"
|
2020-04-30 09:14:46 +00:00
|
|
|
"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-04-30 09:14:46 +00:00
|
|
|
|
2020-06-10 07:52:44 +00:00
|
|
|
// GetAuthProvider get an instance of auth provider based on config
|
2020-04-30 09:14:46 +00:00
|
|
|
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
|
|
|
|
}
|