Dynamic providers list

This commit is contained in:
Meutel 2017-09-09 09:16:46 +02:00
parent 54bf239e38
commit 5e0de4041c
4 changed files with 31 additions and 6 deletions

View File

@ -25,10 +25,23 @@ var (
Providers []OAuth2Provider Providers []OAuth2Provider
) )
// LoginModel is login page model
type LoginModel struct {
Model
Providers []OAuth2Provider
}
// NewLoginModel constructor for LoginModel
func (app *Bouquins) NewLoginModel(req *http.Request) *LoginModel {
return &LoginModel{*app.NewModel("Authentification", "provider", req), Providers}
}
// OAuth2Provider allows to get a user from an OAuth2 token // OAuth2Provider allows to get a user from an OAuth2 token
type OAuth2Provider interface { type OAuth2Provider interface {
GetUser(token *oauth2.Token) (string, error) GetUser(token *oauth2.Token) (string, error)
Name() string Name() string
Label() string
Icon() string
} }
// generates a 16 characters long random string // generates a 16 characters long random string
@ -75,7 +88,7 @@ func (app *Bouquins) LoginPage(res http.ResponseWriter, req *http.Request) error
return nil return nil
} }
// choose provider // choose provider
return app.render(res, tplProvider, app.NewModel("Authentification", "provider", req)) return app.render(res, tplProvider, app.NewLoginModel(req))
} }
// LogoutPage logout connected user // LogoutPage logout connected user

View File

@ -157,7 +157,7 @@ type Model struct {
Username string Username string
} }
// NewModel constuctor for Model // NewModel constructor for Model
func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model { func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model {
return &Model{ return &Model{
Title: title, Title: title,

View File

@ -12,7 +12,7 @@ import (
// GithubProvider implements OAuth2 client with github.com // GithubProvider implements OAuth2 client with github.com
type GithubProvider string type GithubProvider string
type gitHubEmail struct { type githubEmail struct {
Email string `json:"email"` Email string `json:"email"`
Primary bool `json:"primary"` Primary bool `json:"primary"`
Verified bool `json:"verified"` Verified bool `json:"verified"`
@ -28,6 +28,16 @@ func (p GithubProvider) Name() string {
return string(p) return string(p)
} }
// Label returns label of provider
func (p GithubProvider) Label() string {
return "Github"
}
// Icon returns icon path for provider
func (p GithubProvider) Icon() string {
return "" // TODO
}
// GetUser returns github primary email // GetUser returns github primary email
func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) { func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil) apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil)
@ -42,7 +52,7 @@ func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
} }
dec := json.NewDecoder(response.Body) dec := json.NewDecoder(response.Body)
var emails []gitHubEmail var emails []githubEmail
err = dec.Decode(&emails) err = dec.Decode(&emails)
if err != nil { if err != nil {
log.Println("Error reading github API response", err) log.Println("Error reading github API response", err)

View File

@ -2,8 +2,10 @@
<div class="container" id="provider"> <div class="container" id="provider">
<p>Veuillez sélectionner un mode d'authentification:<p> <p>Veuillez sélectionner un mode d'authentification:<p>
<ul> <ul>
<!-- TODO list providers, icon --> {{ range .Providers }}
<li><a href="/login?provider=github">Github</a></li> <!-- TODO icon -->
<li><a href="/login?provider={{ .Name }}">{{ .Label }}</a></li>
{{ end }}
</ul> </ul>
</div> </div>
{{ template "footer.html" . }} {{ template "footer.html" . }}