Refactoring separate file for OAuth2 provider
This commit is contained in:
parent
93a1de6452
commit
db88244873
@ -1,7 +1,6 @@
|
|||||||
package bouquins
|
package bouquins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -22,7 +21,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Providers = [1]OAuth2Provider{GithubProvider("github")}
|
Providers []OAuth2Provider
|
||||||
)
|
)
|
||||||
|
|
||||||
type OAuth2Provider interface {
|
type OAuth2Provider interface {
|
||||||
@ -30,50 +29,6 @@ type OAuth2Provider interface {
|
|||||||
Name() string
|
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
|
// generates a 16 characters long random string
|
||||||
func securedRandString() string {
|
func securedRandString() string {
|
||||||
b := make([]byte, 16)
|
b := make([]byte, 16)
|
||||||
|
59
bouquins/github.go
Normal file
59
bouquins/github.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user