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
)
// 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
type OAuth2Provider interface {
GetUser(token *oauth2.Token) (string, error)
Name() string
Label() string
Icon() 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
}
// choose provider
return app.render(res, tplProvider, app.NewModel("Authentification", "provider", req))
return app.render(res, tplProvider, app.NewLoginModel(req))
}
// LogoutPage logout connected user

View File

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

View File

@ -12,7 +12,7 @@ import (
// GithubProvider implements OAuth2 client with github.com
type GithubProvider string
type gitHubEmail struct {
type githubEmail struct {
Email string `json:"email"`
Primary bool `json:"primary"`
Verified bool `json:"verified"`
@ -28,6 +28,16 @@ func (p GithubProvider) Name() string {
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
func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
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)
var emails []gitHubEmail
var emails []githubEmail
err = dec.Decode(&emails)
if err != nil {
log.Println("Error reading github API response", err)

View File

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