From db88244873a4d6eeb77084f0b118f22dca4e83ed Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 8 Sep 2017 20:33:16 +0200 Subject: [PATCH] Refactoring separate file for OAuth2 provider --- bouquins/auth.go | 47 +----------------------------------- bouquins/github.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 bouquins/github.go diff --git a/bouquins/auth.go b/bouquins/auth.go index 6902397..113adc5 100644 --- a/bouquins/auth.go +++ b/bouquins/auth.go @@ -1,7 +1,6 @@ package bouquins import ( - "encoding/json" "fmt" "log" "math/rand" @@ -22,7 +21,7 @@ const ( ) var ( - Providers = [1]OAuth2Provider{GithubProvider("github")} + Providers []OAuth2Provider ) type OAuth2Provider interface { @@ -30,50 +29,6 @@ type OAuth2Provider interface { Name() string } -type GithubProvider string - -func (p GithubProvider) Name() string { - return string(p) -} -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") - apiReq.Header.Add("Authorization", "token "+token.AccessToken) - client := &http.Client{} - response, err := client.Do(apiReq) - defer response.Body.Close() - if err != nil { - log.Println("Auth error", err) - return "", fmt.Errorf("Authentification error") - } - - dec := json.NewDecoder(response.Body) - var emails []GitHubEmail - err = dec.Decode(&emails) - if err != nil { - log.Println("Error reading github API response", err) - return "", fmt.Errorf("Error reading github API response") - } - fmt.Printf("Content: %s\n", emails) - var userEmail string - for _, email := range emails { - if email.Primary && email.Verified { - userEmail = email.Email - } - } - log.Println("User email:", userEmail) - return userEmail, nil -} - -func findProvider(name string) OAuth2Provider { - for _, p := range Providers { - if p.Name() == name { - return p - } - } - return nil -} - // generates a 16 characters long random string func securedRandString() string { b := make([]byte, 16) diff --git a/bouquins/github.go b/bouquins/github.go new file mode 100644 index 0000000..0cbdddd --- /dev/null +++ b/bouquins/github.go @@ -0,0 +1,59 @@ +package bouquins + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + + "golang.org/x/oauth2" +) + +type GithubProvider string + +func init() { + Providers = append(Providers, GithubProvider("github")) +} + +func (p GithubProvider) Name() string { + return string(p) +} + +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") + apiReq.Header.Add("Authorization", "token "+token.AccessToken) + client := &http.Client{} + response, err := client.Do(apiReq) + defer response.Body.Close() + if err != nil { + log.Println("Auth error", err) + return "", fmt.Errorf("Authentification error") + } + + dec := json.NewDecoder(response.Body) + var emails []GitHubEmail + err = dec.Decode(&emails) + if err != nil { + log.Println("Error reading github API response", err) + return "", fmt.Errorf("Error reading github API response") + } + fmt.Printf("Content: %s\n", emails) + var userEmail string + for _, email := range emails { + if email.Primary && email.Verified { + userEmail = email.Email + } + } + log.Println("User email:", userEmail) + return userEmail, nil +} + +func findProvider(name string) OAuth2Provider { + for _, p := range Providers { + if p.Name() == name { + return p + } + } + return nil +}