Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ And you should get a new user:

### List/Find Users

To create a new user, make a request to `GET /admin/users`.
To list all users, make a request to `GET /admin/users`.

#### Request

Expand Down
2 changes: 2 additions & 0 deletions api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ func (a *API) Provider(ctx context.Context, name string, scopes string) (provide
return provider.NewGitlabProvider(config.External.Gitlab, scopes)
case "google":
return provider.NewGoogleProvider(config.External.Google, scopes)
case "kakao":
return provider.NewKakaoProvider(config.External.Kakao, scopes)
case "linkedin":
return provider.NewLinkedinProvider(config.External.Linkedin, scopes)
case "facebook":
Expand Down
100 changes: 100 additions & 0 deletions api/provider/kakao.go
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 {
Copy link
Member

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 and secure_resource, you can't use the wrapper function makeRequest, you'll need to construct the http request on your own (much like the notion implementation)

Copy link
Author

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

Copy link
Author

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

Copy link
Member

@kangmingtay kangmingtay Feb 7, 2022

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 or log package to print the http response returned from /v2/user/me inside makeRequest

If you want to print the contents of a struct, you can try this:

var u kakaoUser
fmt.Printf("%+v", u)

It might also be worth taking a look at the tour of go to familiarise yourself with the language :)

Copy link
Author

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

return nil, err
}
fmt.Printf("%+v\n", u)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print statement

Copy link
Author

Choose a reason for hiding this comment

The 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
}
2 changes: 2 additions & 0 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ProviderSettings struct {
GitHub bool `json:"github"`
GitLab bool `json:"gitlab"`
Google bool `json:"google"`
Kakao bool `json:"kakao"`
Linkedin bool `json:"linkedin"`
Facebook bool `json:"facebook"`
Notion bool `json:"notion"`
Expand Down Expand Up @@ -47,6 +48,7 @@ func (a *API) Settings(w http.ResponseWriter, r *http.Request) error {
GitHub: config.External.Github.Enabled,
GitLab: config.External.Gitlab.Enabled,
Google: config.External.Google.Enabled,
Kakao: config.External.Kakao.Enabled,
Linkedin: config.External.Linkedin.Enabled,
Facebook: config.External.Facebook.Enabled,
Notion: config.External.Notion.Enabled,
Expand Down
1 change: 1 addition & 0 deletions api/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestSettings_DefaultProviders(t *testing.T) {
require.True(t, p.Spotify)
require.True(t, p.Slack)
require.True(t, p.Google)
require.True(t, p.Kakao)
require.True(t, p.Linkedin)
require.True(t, p.GitHub)
require.True(t, p.GitLab)
Expand Down
1 change: 1 addition & 0 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type ProviderConfiguration struct {
Github OAuthProviderConfiguration `json:"github"`
Gitlab OAuthProviderConfiguration `json:"gitlab"`
Google OAuthProviderConfiguration `json:"google"`
Kakao OAuthProviderConfiguration `json:"kakao"`
Notion OAuthProviderConfiguration `json:"notion"`
Linkedin OAuthProviderConfiguration `json:"linkedin"`
Spotify OAuthProviderConfiguration `json:"spotify"`
Expand Down
6 changes: 6 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ GOTRUE_EXTERNAL_GOOGLE_CLIENT_ID=""
GOTRUE_EXTERNAL_GOOGLE_SECRET=""
GOTRUE_EXTERNAL_GOOGLE_REDIRECT_URI="http://localhost:9999/callback"

# Kakao OAuth config
GOTRUE_EXTERNAL_KAKAO_ENABLED="true"
GOTRUE_EXTERNAL_KAKAO_CLIENT_ID=""
GOTRUE_EXTERNAL_KAKAO_SECRET=""
GOTRUE_EXTERNAL_KAKAO_REDIRECT_URI="http://localhost:9999/callback"

# Github OAuth config
GOTRUE_EXTERNAL_GITHUB_ENABLED="false"
GOTRUE_EXTERNAL_GITHUB_CLIENT_ID=""
Expand Down
45 changes: 21 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
module github.com/netlify/gotrue

require (
cloud.google.com/go v0.67.0 // indirect
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170623214735-571947b0f240
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/badoux/checkmail v0.0.0-20170203135005-d0a759655d62
github.com/beevik/etree v1.1.0
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-oidc/v3 v3.0.0
github.com/didip/tollbooth/v5 v5.1.1
github.com/fatih/color v1.10.0 // indirect
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/envy v1.9.0 // indirect
github.com/gobuffalo/fizz v1.13.0 // indirect
github.com/gobuffalo/flect v0.2.2 // indirect
github.com/gobuffalo/nulls v0.4.0 // indirect
github.com/gobuffalo/packr/v2 v2.8.1 // indirect
github.com/gobuffalo/plush/v4 v4.1.0 // indirect
github.com/gobuffalo/pop/v5 v5.3.3
github.com/gobuffalo/validate/v3 v3.3.0 // indirect
github.com/gofrs/uuid v4.0.0+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/gobuffalo/fizz v1.14.0 // indirect
github.com/gobuffalo/packr/v2 v2.8.3 // indirect
github.com/gobuffalo/pop/v5 v5.3.4
github.com/gobuffalo/pop/v6 v6.0.1 // indirect
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.1.1
github.com/gorilla/websocket v1.4.2 // indirect
github.com/imdario/mergo v0.0.0-20160216103600-3e95a51e0639
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jmoiron/sqlx v1.3.1 // indirect
github.com/joho/godotenv v1.3.0
github.com/jackc/pgx v3.6.2+incompatible // indirect
github.com/jackc/pgx/v4 v4.14.1 // indirect
github.com/joho/godotenv v1.4.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/lestrrat-go/jwx v0.9.0
github.com/lib/pq v1.9.0 // indirect
github.com/microcosm-cc/bluemonday v1.0.16 // indirect
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
github.com/mrjones/oauth v0.0.0-20190623134757-126b35219450
github.com/netlify/mailme v1.1.1
github.com/opentracing/opentracing-go v1.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/rs/cors v1.6.0
github.com/russellhaering/gosaml2 v0.6.1-0.20210916051624-757d23f1bc28
github.com/russellhaering/goxmldsig v1.1.1
github.com/sebest/xff v0.0.0-20160910043805-6c115e0ffa35
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/net v0.0.0-20220121210141-e204ce36a2ba // indirect
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/tools v0.1.9 // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.12.1
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
Expand Down
Loading