initial commit
This commit is contained in:
74
internal/pkg/oauth/linkedin/linkedin.go
Normal file
74
internal/pkg/oauth/linkedin/linkedin.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package linkedin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/linkedin"
|
||||
"io"
|
||||
|
||||
"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: linkedin.Endpoint,
|
||||
RedirectURL: config.RedirectURL,
|
||||
Scopes: config.Scopes,
|
||||
}
|
||||
return &client{oauthConfig: oauthConfig}
|
||||
}
|
||||
|
||||
func (l client) GetConsentAuthUrl(ctx context.Context, state string) string {
|
||||
return l.oauthConfig.AuthCodeURL(state, oauth2.AccessTypeOffline)
|
||||
}
|
||||
|
||||
func (l client) ExchangeCodeWithToken(ctx context.Context, code string) (*types.Token, error) {
|
||||
exchange, err := l.oauthConfig.Exchange(ctx, code, oauth2.AccessTypeOffline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token, err := l.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 (l client) GetUserInfo(
|
||||
ctx context.Context,
|
||||
accessToken string,
|
||||
refreshToken string,
|
||||
) (types.UserInfo, error) {
|
||||
resp, err := l.oauthConfig.Client(ctx, &oauth2.Token{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
}).Get("https://api.linkedin.com/v2/me")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var user UserInfo
|
||||
|
||||
if err = json.Unmarshal(data, &user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
57
internal/pkg/oauth/linkedin/user.go
Normal file
57
internal/pkg/oauth/linkedin/user.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package linkedin
|
||||
|
||||
type UserInfo struct {
|
||||
Id string `json:"id"`
|
||||
LocalizedFirstName string `json:"localizedFirstName"`
|
||||
LocalizedHeadline string `json:"localizedHeadline"`
|
||||
VanityName string `json:"vanityName"`
|
||||
LocalizedLastName string `json:"localizedLastName"`
|
||||
Firstname UserInfoFirstName `json:"firstName"`
|
||||
Lastname UserInfoLastName `json:"lastName"`
|
||||
Headline UserInfoHeadline `json:"headline"`
|
||||
ProfilePicture UserInfoProfilePicture `json:"profilePicture"`
|
||||
}
|
||||
|
||||
type UserInfoFirstName struct {
|
||||
Localized Localized `json:"localized"`
|
||||
PreferredLocale PreferredLocale `json:"preferredLocale"`
|
||||
}
|
||||
|
||||
type UserInfoLastName struct {
|
||||
Localized Localized `json:"localized"`
|
||||
PreferredLocale PreferredLocale `json:"preferredLocale"`
|
||||
}
|
||||
|
||||
type Localized struct {
|
||||
EnUS string `json:"en_US"`
|
||||
}
|
||||
|
||||
type PreferredLocale struct {
|
||||
Country string `json:"country"`
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
type UserInfoHeadline struct {
|
||||
Localized Localized `json:"localized"`
|
||||
PreferredLocale PreferredLocale `json:"preferredLocale"`
|
||||
}
|
||||
|
||||
type UserInfoProfilePicture struct {
|
||||
DisplayImage string `json:"displayImage"`
|
||||
}
|
||||
|
||||
func (u UserInfo) ID() string {
|
||||
return u.Id
|
||||
}
|
||||
|
||||
func (u UserInfo) Email() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u UserInfo) FirstName() string {
|
||||
return u.Firstname.Localized.EnUS
|
||||
}
|
||||
|
||||
func (u UserInfo) LastName() string {
|
||||
return u.Lastname.Localized.EnUS
|
||||
}
|
||||
Reference in New Issue
Block a user