From 54bf239e3807c1cd30d1da97e38773a604442d28 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 8 Sep 2017 20:41:30 +0200 Subject: [PATCH] golint compliance --- bouquins/auth.go | 11 ++++++----- bouquins/bouquins.go | 8 +------- bouquins/github.go | 12 +++++++++++- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/bouquins/auth.go b/bouquins/auth.go index 113adc5..bc71125 100644 --- a/bouquins/auth.go +++ b/bouquins/auth.go @@ -21,9 +21,11 @@ const ( ) var ( + // Providers contains OAuth2 providers implementations Providers []OAuth2Provider ) +// OAuth2Provider allows to get a user from an OAuth2 token type OAuth2Provider interface { GetUser(token *oauth2.Token) (string, error) Name() string @@ -38,13 +40,13 @@ func securedRandString() string { return string(b) } -// current session +// Session returns current session func (app *Bouquins) Session(req *http.Request) *sessions.Session { session, _ := app.Cookies.Get(req, sessionName) return session } -// logged in username +// Username returns logged in username func (app *Bouquins) Username(req *http.Request) string { username := app.Session(req).Values[sessionUser] if username != nil { @@ -53,7 +55,7 @@ func (app *Bouquins) Username(req *http.Request) string { 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) { session := app.Session(req) 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) log.Println("User logged in", userEmail) return RedirectHome(res, req) - } else { - return fmt.Errorf("Unknown user") } + return fmt.Errorf("Unknown user") } diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index 72373d2..4da9dea 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -19,6 +19,7 @@ import ( ) const ( + // Version defines application version Version = "master" tplBooks = "book.html" @@ -64,13 +65,6 @@ const ( 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 type Bouquins struct { Tpl *template.Template diff --git a/bouquins/github.go b/bouquins/github.go index 0cbdddd..fc065f0 100644 --- a/bouquins/github.go +++ b/bouquins/github.go @@ -9,16 +9,26 @@ import ( "golang.org/x/oauth2" ) +// GithubProvider implements OAuth2 client with github.com type GithubProvider string +type gitHubEmail struct { + Email string `json:"email"` + Primary bool `json:"primary"` + Verified bool `json:"verified"` + Visibility string `json:"visibility"` +} + func init() { Providers = append(Providers, GithubProvider("github")) } +// Name returns name of provider func (p GithubProvider) Name() string { return string(p) } +// 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) 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) - var emails []GitHubEmail + var emails []gitHubEmail err = dec.Decode(&emails) if err != nil { log.Println("Error reading github API response", err)