-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1463 from teamhanko/feat/1452-linkedin-provider
feat(thirdparty): add linkedin to social providers
- Loading branch information
Showing
9 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package thirdparty | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/coreos/go-oidc/v3/oidc" | ||
"github.com/teamhanko/hanko/backend/config" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
LinkedInIssuer = "https://www.linkedin.com/oauth" | ||
) | ||
|
||
var DefaultLinkedinScopes = []string{ | ||
"openid", | ||
"profile", | ||
"email", | ||
} | ||
|
||
type LinkedinUser struct { | ||
ID string `json:"sub"` | ||
Name string `json:"name"` | ||
GivenName string `json:"given_name"` | ||
FamilyName string `json:"family_name"` | ||
Picture string `json:"picture"` | ||
Locale struct { | ||
Country string `json:"country"` | ||
Language string `json:"language"` | ||
} `json:"locale"` | ||
Email string `json:"email"` | ||
Verified bool `json:"email_verified"` | ||
} | ||
|
||
type linkedInProvider struct { | ||
oidc *oidc.Provider | ||
*oauth2.Config | ||
} | ||
|
||
// NewLinkedInProvider creates a LinkedIn third party provider. | ||
func NewLinkedInProvider(config config.ThirdPartyProvider, redirectURL string) (OAuthProvider, error) { | ||
if !config.Enabled { | ||
return nil, errors.New("linkedIn provider is disabled") | ||
} | ||
|
||
oidcProvider, err := oidc.NewProvider(context.Background(), LinkedInIssuer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
endpoint := oidcProvider.Endpoint() | ||
|
||
return &linkedInProvider{ | ||
oidc: oidcProvider, | ||
Config: &oauth2.Config{ | ||
ClientID: config.ClientID, | ||
ClientSecret: config.Secret, | ||
Endpoint: endpoint, | ||
Scopes: DefaultLinkedinScopes, | ||
RedirectURL: redirectURL, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (g linkedInProvider) GetOAuthToken(code string) (*oauth2.Token, error) { | ||
return g.Exchange(context.Background(), code) | ||
} | ||
|
||
func (g linkedInProvider) GetUserData(token *oauth2.Token) (*UserData, error) { | ||
var user LinkedinUser | ||
if err := makeRequest(token, g.Config, g.oidc.UserInfoEndpoint(), &user); err != nil { | ||
return nil, err | ||
} | ||
|
||
data := &UserData{} | ||
|
||
if user.Email != "" { | ||
data.Emails = append(data.Emails, Email{ | ||
Email: user.Email, | ||
Verified: user.Verified, | ||
Primary: true, | ||
}) | ||
} | ||
|
||
if len(data.Emails) <= 0 { | ||
return nil, errors.New("unable to find email with LinkedIn provider") | ||
} | ||
|
||
data.Metadata = &Claims{ | ||
Issuer: LinkedInIssuer, | ||
Subject: user.ID, | ||
Name: user.Name, | ||
FamilyName: user.FamilyName, | ||
GivenName: user.GivenName, | ||
Picture: user.Picture, | ||
Locale: fmt.Sprintf("%s-%s", user.Locale.Country, user.Locale.Language), | ||
Email: user.Email, | ||
EmailVerified: user.Verified, | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (g linkedInProvider) Name() string { | ||
return "linkedin" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { IconProps } from "./Icon"; | ||
import cx from "classnames"; | ||
import styles from "./styles.sass"; | ||
|
||
const LinkedIn = ({ size, secondary, disabled }: IconProps) => { | ||
return ( | ||
<svg | ||
id="icon-linkedin" | ||
fill="#fff" | ||
xmlns="http://www.w3.org/2000/svg" | ||
width={size} | ||
viewBox="0 0 24 24" | ||
height={size} | ||
className={cx( | ||
styles.icon, | ||
secondary && styles.secondary, | ||
disabled && styles.disabled, | ||
)} | ||
> | ||
<path d="M20.5 2h-17A1.5 1.5 0 002 3.5v17A1.5 1.5 0 003.5 22h17a1.5 1.5 0 001.5-1.5v-17A1.5 1.5 0 0020.5 2zM8 19H5v-9h3zM6.5 8.25A1.75 1.75 0 118.3 6.5a1.78 1.78 0 01-1.8 1.75zM19 19h-3v-4.74c0-1.42-.6-1.93-1.38-1.93A1.74 1.74 0 0013 14.19a.66.66 0 000 .14V19h-3v-9h2.9v1.3a3.11 3.11 0 012.7-1.4c1.55 0 3.36.86 3.36 3.66z" /> | ||
</svg> | ||
); | ||
}; | ||
|
||
export default LinkedIn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters