initial commit
This commit is contained in:
107
internal/pkg/oauth/github/client.go
Normal file
107
internal/pkg/oauth/github/client.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/github"
|
||||
|
||||
"base/internal/pkg/oauth/types"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
oauthConfig *oauth2.Config
|
||||
}
|
||||
|
||||
func New(config oauth2.Config) types.Oauth {
|
||||
oauthConfig := &oauth2.Config{
|
||||
ClientID: config.ClientID,
|
||||
ClientSecret: config.ClientSecret,
|
||||
Endpoint: github.Endpoint,
|
||||
RedirectURL: config.RedirectURL,
|
||||
Scopes: config.Scopes,
|
||||
}
|
||||
return &client{oauthConfig: oauthConfig}
|
||||
}
|
||||
|
||||
func (g client) GetConsentAuthUrl(ctx context.Context, state string) string {
|
||||
return g.oauthConfig.AuthCodeURL(state, oauth2.AccessTypeOffline)
|
||||
}
|
||||
|
||||
func (g client) ExchangeCodeWithToken(ctx context.Context, code string) (*types.Token, error) {
|
||||
exchange, err := g.oauthConfig.Exchange(ctx, code, oauth2.AccessTypeOffline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token, err := g.oauthConfig.TokenSource(ctx, exchange).Token()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &types.Token{
|
||||
AccessToken: token.AccessToken,
|
||||
TokenType: token.TokenType,
|
||||
RefreshToken: token.RefreshToken,
|
||||
ExpiresIn: token.ExpiresIn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g client) GetUserInfo(ctx context.Context, token, _ string) (types.UserInfo, error) {
|
||||
oauthClient := g.oauthConfig.Client(ctx, &oauth2.Token{AccessToken: token})
|
||||
|
||||
resp, err := oauthClient.Get("https://api.github.com/user")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, readErr := io.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
return nil, readErr
|
||||
}
|
||||
|
||||
var user UserInfo
|
||||
if err = json.Unmarshal(data, &user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// GitHub /user often returns null for email; fetch from /user/emails (requires user:email scope)
|
||||
if user.GEmail == "" {
|
||||
user.GEmail = g.fetchPrimaryEmail(ctx, oauthClient)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// fetchPrimaryEmail gets the primary email from GitHub /user/emails (requires user:email scope).
|
||||
func (g client) fetchPrimaryEmail(_ context.Context, oauthClient *http.Client) string {
|
||||
resp, err := oauthClient.Get("https://api.github.com/user/emails")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var emails []struct {
|
||||
Email string `json:"email"`
|
||||
Primary bool `json:"primary"`
|
||||
Verified bool `json:"verified"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &emails); err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, e := range emails {
|
||||
if e.Primary && e.Verified {
|
||||
return e.Email
|
||||
}
|
||||
}
|
||||
if len(emails) > 0 {
|
||||
return emails[0].Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
59
internal/pkg/oauth/github/user.go
Normal file
59
internal/pkg/oauth/github/user.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
Login string `json:"login"`
|
||||
Id int `json:"id"`
|
||||
NodeId string `json:"node_id"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
GravatarId string `json:"gravatar_id"`
|
||||
Url string `json:"url"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
FollowersUrl string `json:"followers_url"`
|
||||
FollowingUrl string `json:"following_url"`
|
||||
GistsUrl string `json:"gists_url"`
|
||||
StarredUrl string `json:"starred_url"`
|
||||
SubscriptionsUrl string `json:"subscriptions_url"`
|
||||
OrganizationsUrl string `json:"organizations_url"`
|
||||
ReposUrl string `json:"repos_url"`
|
||||
EventsUrl string `json:"events_url"`
|
||||
ReceivedEventsUrl string `json:"received_events_url"`
|
||||
Type string `json:"type"`
|
||||
UserViewType string `json:"user_view_type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
Name string `json:"name"`
|
||||
Company interface{} `json:"company"`
|
||||
Blog string `json:"blogusecase"`
|
||||
Location interface{} `json:"location"`
|
||||
GEmail string `json:"email"`
|
||||
Hireable interface{} `json:"hireable"`
|
||||
Bio string `json:"bio"`
|
||||
TwitterUsername string `json:"twitter_username"`
|
||||
NotificationEmail string `json:"notification_email"`
|
||||
PublicRepos int `json:"public_repos"`
|
||||
PublicGists int `json:"public_gists"`
|
||||
Followers int `json:"followers"`
|
||||
Following int `json:"following"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (u UserInfo) ID() string {
|
||||
return fmt.Sprintf("%d", u.Id)
|
||||
}
|
||||
|
||||
func (u UserInfo) Email() string {
|
||||
return u.GEmail
|
||||
}
|
||||
|
||||
func (u UserInfo) FirstName() string {
|
||||
return u.Name
|
||||
}
|
||||
|
||||
func (u UserInfo) LastName() string {
|
||||
return u.Name
|
||||
}
|
||||
Reference in New Issue
Block a user