golint compliance

This commit is contained in:
Meutel 2017-09-08 20:41:30 +02:00
parent db88244873
commit 54bf239e38
3 changed files with 18 additions and 13 deletions

View File

@ -21,9 +21,11 @@ const (
) )
var ( var (
// Providers contains OAuth2 providers implementations
Providers []OAuth2Provider Providers []OAuth2Provider
) )
// 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
@ -38,13 +40,13 @@ func securedRandString() string {
return string(b) return string(b)
} }
// current session // Session returns current session
func (app *Bouquins) Session(req *http.Request) *sessions.Session { func (app *Bouquins) Session(req *http.Request) *sessions.Session {
session, _ := app.Cookies.Get(req, sessionName) session, _ := app.Cookies.Get(req, sessionName)
return session return session
} }
// logged in username // Username returns logged in username
func (app *Bouquins) Username(req *http.Request) string { func (app *Bouquins) Username(req *http.Request) string {
username := app.Session(req).Values[sessionUser] username := app.Session(req).Values[sessionUser]
if username != nil { if username != nil {
@ -53,7 +55,7 @@ func (app *Bouquins) Username(req *http.Request) string {
return "" return ""
} }
// sets value in session // SessionSet sets a value in session
func (app *Bouquins) SessionSet(name string, value string, res http.ResponseWriter, req *http.Request) { func (app *Bouquins) SessionSet(name string, value string, res http.ResponseWriter, req *http.Request) {
session := app.Session(req) session := app.Session(req)
session.Values[name] = value session.Values[name] = value
@ -115,7 +117,6 @@ func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) er
app.SessionSet(sessionUser, "Meutel", res, req) app.SessionSet(sessionUser, "Meutel", res, req)
log.Println("User logged in", userEmail) log.Println("User logged in", userEmail)
return RedirectHome(res, req) return RedirectHome(res, req)
} else {
return fmt.Errorf("Unknown user")
} }
return fmt.Errorf("Unknown user")
} }

View File

@ -19,6 +19,7 @@ import (
) )
const ( const (
// Version defines application version
Version = "master" Version = "master"
tplBooks = "book.html" tplBooks = "book.html"
@ -64,13 +65,6 @@ const (
URLCalibre = "/calibre/" URLCalibre = "/calibre/"
) )
type GitHubEmail struct {
Email string `json:"email"`
Primary bool `json:"primary"`
Verified bool `json:"verified"`
Visibility string `json:"visibility"`
}
// Bouquins contains application common resources: templates, database // Bouquins contains application common resources: templates, database
type Bouquins struct { type Bouquins struct {
Tpl *template.Template Tpl *template.Template

View File

@ -9,16 +9,26 @@ import (
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
// GithubProvider implements OAuth2 client with github.com
type GithubProvider string type GithubProvider string
type gitHubEmail struct {
Email string `json:"email"`
Primary bool `json:"primary"`
Verified bool `json:"verified"`
Visibility string `json:"visibility"`
}
func init() { func init() {
Providers = append(Providers, GithubProvider("github")) Providers = append(Providers, GithubProvider("github"))
} }
// Name returns name of provider
func (p GithubProvider) Name() string { func (p GithubProvider) Name() string {
return string(p) return string(p)
} }
// 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)
apiReq.Header.Add("Accept", "application/vnd.github.v3+json") apiReq.Header.Add("Accept", "application/vnd.github.v3+json")
@ -32,7 +42,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)