-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new provider: kakao #366
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/netlify/gotrue/conf" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
defaultKakaoApiBase = "kauth.kakao.com" | ||
defaultKakaoAPIBase = "kapi.kakao.com" | ||
) | ||
|
||
type kakaoProvider struct { | ||
*oauth2.Config | ||
APIPath string | ||
} | ||
|
||
type kakaoUser struct { | ||
ID string `json:"id"` | ||
KakaoAccount struct { | ||
Email string `json:"email"` | ||
EmailVerified bool `json:"is_email_verifieds"` | ||
Name string `json:"name"` | ||
Profile struct { | ||
Nickname string `json:"nickname"` | ||
AvatarURL string `json:"profile_image_url"` | ||
} `json:"profile"` | ||
} `json:"kakao_account"` | ||
} | ||
|
||
// NewKakaoProvider creates a Kakao account provider. | ||
func NewKakaoProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) { | ||
if err := ext.Validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
apiPath := chooseHost(ext.URL, defaultKakaoApiBase) | ||
authPath := chooseHost(ext.URL, defaultKakaoApiBase) | ||
|
||
oauthScopes := []string{ | ||
"profile_image", | ||
"profile_nickname", | ||
"account_email", | ||
} | ||
|
||
if scopes != "" { | ||
oauthScopes = append(oauthScopes, strings.Split(scopes, ",")...) | ||
} | ||
|
||
return &kakaoProvider{ | ||
Config: &oauth2.Config{ | ||
ClientID: ext.ClientID, | ||
ClientSecret: ext.Secret, | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: authPath + "/oauth/authorize", | ||
TokenURL: apiPath + "/oauth/token", | ||
}, | ||
Scopes: oauthScopes, | ||
RedirectURL: ext.RedirectURI, | ||
}, | ||
APIPath: apiPath, | ||
}, nil | ||
} | ||
|
||
func (g kakaoProvider) GetOAuthToken(code string) (*oauth2.Token, error) { | ||
return g.Exchange(oauth2.NoContext, code) | ||
} | ||
|
||
func (g kakaoProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*UserProvidedData, error) { | ||
var u kakaoUser | ||
if err := makeRequest(ctx, tok, g.Config, g.APIPath+"/v2/user/me", &u); err != nil { | ||
return nil, err | ||
} | ||
fmt.Printf("%+v\n", u) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove print statement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks I will check the contributing guide again :D |
||
|
||
if u.KakaoAccount.Email == "" { | ||
return nil, errors.New("Unable to find email with Kakao provider") | ||
} | ||
|
||
return &UserProvidedData{ | ||
Metadata: &Claims{ | ||
Issuer: g.APIPath, | ||
Subject: u.ID, | ||
Name: u.KakaoAccount.Name, | ||
Picture: u.KakaoAccount.Profile.AvatarURL, | ||
Email: u.KakaoAccount.Email, | ||
EmailVerified: u.KakaoAccount.EmailVerified, | ||
}, | ||
Emails: []Email{{ | ||
Email: u.KakaoAccount.Email, | ||
Verified: u.KakaoAccount.EmailVerified, | ||
Primary: true, | ||
}}, | ||
}, nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the kakao api requires the developer to pass 2 additional parameters:
property_keys
andsecure_resource
, you can't use the wrapper functionmakeRequest
, you'll need to construct the http request on your own (much like the notion implementation)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I will refer the notion implementation to fix it. :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kangmingtay I am actually not familiar with golang, could you give me a help to write a debug code?
I was trying to print some log but I couldn't figure out how to do it.
Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can either use the
fmt
orlog
package to print the http response returned from/v2/user/me
insidemakeRequest
If you want to print the contents of a struct, you can try this:
It might also be worth taking a look at the tour of go to familiarise yourself with the language :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you very much :D