Move Oauth configs to the database

This commit is contained in:
marcos
2017-12-18 12:14:21 -03:00
parent 6a1e91b1e9
commit 5332f954dd
4 changed files with 100 additions and 78 deletions

View File

@@ -12,6 +12,7 @@ import (
"time"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/oauth2"
gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
@@ -19,9 +20,12 @@ import (
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
oauth2FB "golang.org/x/oauth2/facebook"
oauth2Github "golang.org/x/oauth2/github"
)
var core pwd.PWDApi
@@ -47,7 +51,7 @@ func Bootstrap(c pwd.PWDApi, ev event.EventApi) {
}
func Register(extend HandlerExtender) {
initLandings()
initPlaygrounds()
r := mux.NewRouter()
corsRouter := mux.NewRouter()
@@ -168,28 +172,73 @@ func Register(extend HandlerExtender) {
}
}
func initLandings() {
func initPlaygrounds() {
pgs, err := core.PlaygroundList()
if err != nil {
log.Fatal("Error getting playgrounds to initialize landings")
log.Fatal("Error getting playgrounds for initialization")
}
for _, p := range pgs {
if p.AssetsDir == "" {
p.AssetsDir = "default"
}
var b bytes.Buffer
t, err := template.New("landing.html").Delims("[[", "]]").ParseFiles(fmt.Sprintf("./www/%s/landing.html", p.AssetsDir))
if err != nil {
log.Fatalf("Error parsing template %v", err)
}
if err := t.Execute(&b, struct{ SegmentId string }{config.SegmentId}); err != nil {
log.Fatalf("Error executing template %v", err)
}
landingBytes, err := ioutil.ReadAll(&b)
if err != nil {
log.Fatalf("Error reading template bytes %v", err)
}
landings[p.Id] = landingBytes
for _, p := range pgs {
initAssets(p)
initOauthProviders(p)
}
}
func initAssets(p *types.Playground) {
if p.AssetsDir == "" {
p.AssetsDir = "default"
}
var b bytes.Buffer
t, err := template.New("landing.html").Delims("[[", "]]").ParseFiles(fmt.Sprintf("./www/%s/landing.html", p.AssetsDir))
if err != nil {
log.Fatalf("Error parsing template %v", err)
}
if err := t.Execute(&b, struct{ SegmentId string }{config.SegmentId}); err != nil {
log.Fatalf("Error executing template %v", err)
}
landingBytes, err := ioutil.ReadAll(&b)
if err != nil {
log.Fatalf("Error reading template bytes %v", err)
}
landings[p.Id] = landingBytes
}
func initOauthProviders(p *types.Playground) {
config.Providers[p.Id] = map[string]*oauth2.Config{}
if p.GithubClientID != "" && p.GithubClientSecret != "" {
conf := &oauth2.Config{
ClientID: p.GithubClientID,
ClientSecret: p.GithubClientSecret,
Scopes: []string{"user:email"},
Endpoint: oauth2Github.Endpoint,
}
config.Providers[p.Id]["github"] = conf
}
if p.FacebookClientID != "" && p.FacebookClientSecret != "" {
conf := &oauth2.Config{
ClientID: p.FacebookClientID,
ClientSecret: p.FacebookClientSecret,
Scopes: []string{"email", "public_profile"},
Endpoint: oauth2FB.Endpoint,
}
config.Providers[p.Id]["facebook"] = conf
}
if p.DockerClientID != "" && p.DockerClientSecret != "" {
oauth2.RegisterBrokenAuthHeaderProvider(".id.docker.com")
conf := &oauth2.Config{
ClientID: p.DockerClientID,
ClientSecret: p.DockerClientSecret,
Scopes: []string{"openid"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://id.docker.com/id/oauth/authorize/",
TokenURL: "https://id.docker.com/id/oauth/token",
},
}
config.Providers[p.Id]["docker"] = conf
}
}

View File

@@ -34,8 +34,15 @@ func LoggedInUser(rw http.ResponseWriter, req *http.Request) {
}
func ListProviders(rw http.ResponseWriter, req *http.Request) {
playground := core.PlaygroundFindByDomain(req.Host)
if playground == nil {
log.Printf("Playground for domain %s was not found!", req.Host)
rw.WriteHeader(http.StatusBadRequest)
return
}
providers := []string{}
for name, _ := range config.Providers {
for name, _ := range config.Providers[playground.Id] {
providers = append(providers, name)
}
json.NewEncoder(rw).Encode(providers)
@@ -44,8 +51,14 @@ func ListProviders(rw http.ResponseWriter, req *http.Request) {
func Login(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
providerName := vars["provider"]
playground := core.PlaygroundFindByDomain(req.Host)
if playground == nil {
log.Printf("Playground for domain %s was not found!", req.Host)
rw.WriteHeader(http.StatusBadRequest)
return
}
provider, found := config.Providers[providerName]
provider, found := config.Providers[playground.Id][providerName]
if !found {
log.Printf("Could not find provider %s\n", providerName)
rw.WriteHeader(http.StatusNotFound)
@@ -76,8 +89,14 @@ func Login(rw http.ResponseWriter, req *http.Request) {
func LoginCallback(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
providerName := vars["provider"]
playground := core.PlaygroundFindByDomain(req.Host)
if playground == nil {
log.Printf("Playground for domain %s was not found!", req.Host)
rw.WriteHeader(http.StatusBadRequest)
return
}
provider, found := config.Providers[providerName]
provider, found := config.Providers[playground.Id][providerName]
if !found {
log.Printf("Could not find provider %s\n", providerName)
rw.WriteHeader(http.StatusNotFound)