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

feat: Add gender, phone, birthdate metadata by kakao provider #1463

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions internal/api/external_kakao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ func KakaoTestSignupSetup(ts *ExternalTestSuite, tokenCount *int, userCount *int
},
"email": "%v",
"is_email_valid": %v,
"is_email_verified": %v
"is_email_verified": %v,
"name": "김카카오",
"phone_number": "+82 10-1234-5678",
"birthyear": "1991",
"birthday": "1231",
"gender": "male"
}
}`, email.Email, email.Verified, email.Verified)
} else {
Expand All @@ -87,6 +92,11 @@ func KakaoTestSignupSetup(ts *ExternalTestSuite, tokenCount *int, userCount *int
"nickname":"Kakao Test",
"profile_image_url":"http://example.com/avatar"
}
"name": "김카카오",
"phone_number": "+82 10-1234-5678",
"birthyear": "1991",
"birthday": "1231",
"gender": "male"
}
}`)
}
Expand All @@ -106,7 +116,19 @@ func (ts *ExternalTestSuite) TestSignupExternalKakao_AuthorizationCode() {
server := KakaoTestSignupSetup(ts, &tokenCount, &userCount, code, emails)
defer server.Close()
u := performAuthorization(ts, "kakao", code, "")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Kakao Test", "123", "http://example.com/avatar")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "김카카오", "123", "http://example.com/avatar")
}

func (ts *ExternalTestSuite) TestSignupExternalKakaoSuccessWithGenderAndBirthdateAndPhone() {
tokenCount, userCount := 0, 0
code := "authcode"
emails := `[{"email":"[email protected]", "primary": true, "verified": true}]`
server := KakaoTestSignupSetup(ts, &tokenCount, &userCount, code, emails)
defer server.Close()

u := performAuthorization(ts, "kakao", code, "")

assertAuthorizationSuccessWithGenderAndBirthdateAndPhone(ts, u, tokenCount, userCount, "[email protected]", "김카카오", "123", "http://example.com/avatar", "male", "19911231", "+82 10-1234-5678")
}

func (ts *ExternalTestSuite) TestSignupExternalKakaoDisableSignupErrorWhenNoUser() {
Expand Down Expand Up @@ -148,7 +170,7 @@ func (ts *ExternalTestSuite) TestSignupExternalKakaoDisableSignupSuccessWithPrim

u := performAuthorization(ts, "kakao", code, "")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Kakao Test", "123", "http://example.com/avatar")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "김카카오", "123", "http://example.com/avatar")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalKakaoSuccessWhenMatchingToken() {
Expand All @@ -163,7 +185,7 @@ func (ts *ExternalTestSuite) TestInviteTokenExternalKakaoSuccessWhenMatchingToke

u := performAuthorization(ts, "kakao", code, "invite_token")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Kakao Test", "123", "http://example.com/avatar")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "김카카오", "123", "http://example.com/avatar")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalKakaoErrorWhenNoMatchingToken() {
Expand Down Expand Up @@ -225,7 +247,7 @@ func (ts *ExternalTestSuite) TestSignupExternalKakaoErrorWhenUserBanned() {
defer server.Close()

u := performAuthorization(ts, "kakao", code, "")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Kakao Test", "123", "http://example.com/avatar")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "김카카오", "123", "http://example.com/avatar")

user, err := models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
Expand Down
34 changes: 34 additions & 0 deletions internal/api/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,40 @@ func assertAuthorizationSuccess(ts *ExternalTestSuite, u *url.URL, tokenCount in
}
}

func assertAuthorizationSuccessWithGenderAndBirthdateAndPhone(ts *ExternalTestSuite, u *url.URL, tokenCount int, userCount int, email string, name string, providerId string, avatar string, gender, birthdate, phone string) {
// ensure redirect has #access_token=...
v, err := url.ParseQuery(u.RawQuery)
ts.Require().NoError(err)
ts.Require().Empty(v.Get("error_description"))
ts.Require().Empty(v.Get("error"))

v, err = url.ParseQuery(u.Fragment)
ts.Require().NoError(err)
ts.NotEmpty(v.Get("access_token"))
ts.NotEmpty(v.Get("refresh_token"))
ts.NotEmpty(v.Get("expires_in"))
ts.Equal("bearer", v.Get("token_type"))

ts.Equal(1, tokenCount)
if userCount > -1 {
ts.Equal(1, userCount)
}

// ensure user has been created with metadata
user, err := models.FindUserByEmailAndAudience(ts.API.db, email, ts.Config.JWT.Aud)
ts.Require().NoError(err)
ts.Equal(providerId, user.UserMetaData["provider_id"])
ts.Equal(name, user.UserMetaData["full_name"])
if avatar == "" {
ts.Equal(nil, user.UserMetaData["avatar_url"])
} else {
ts.Equal(avatar, user.UserMetaData["avatar_url"])
}
ts.Equal(gender, user.UserMetaData["gender"])
ts.Equal(birthdate, user.UserMetaData["birthdate"])
ts.Equal(phone, user.UserMetaData["phone"])

}
func assertAuthorizationFailure(ts *ExternalTestSuite, u *url.URL, errorDescription string, errorType string, email string) {
// ensure new sign ups error
v, err := url.ParseQuery(u.RawQuery)
Expand Down
42 changes: 37 additions & 5 deletions internal/api/provider/kakao.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ type kakaoUser struct {
ID int `json:"id"`
Account struct {
Profile struct {
Name string `json:"nickname"`
Nickname string `json:"nickname"`
ProfileImageURL string `json:"profile_image_url"`
} `json:"profile"`
Name string `json:"name"`
Email string `json:"email"`
Gender string `json:"gender"`
Phone string `json:"phone_number"`
BirthYear string `json:"birthyear"`
BirthDay string `json:"birthday"`
EmailValid bool `json:"is_email_valid"`
EmailVerified bool `json:"is_email_verified"`
} `json:"kakao_account"`
Expand Down Expand Up @@ -59,15 +64,42 @@ func (p kakaoProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*Use
Issuer: p.APIHost,
Subject: strconv.Itoa(u.ID),

Name: u.Account.Profile.Name,
PreferredUsername: u.Account.Profile.Name,
Name: u.Account.Profile.Nickname,
NickName: u.Account.Profile.Nickname,
PreferredUsername: u.Account.Profile.Nickname,

// To be deprecated
AvatarURL: u.Account.Profile.ProfileImageURL,
FullName: u.Account.Profile.Name,
FullName: u.Account.Profile.Nickname,
ProviderId: strconv.Itoa(u.ID),
UserNameKey: u.Account.Profile.Name,
UserNameKey: u.Account.Profile.Nickname,
}

if u.Account.Name != "" {
data.Metadata.Name = u.Account.Name
data.Metadata.FullName = u.Account.Name
data.Metadata.PreferredUsername = u.Account.Name
data.Metadata.UserNameKey = u.Account.Name
}

if u.Account.Gender != "" {
data.Metadata.Gender = u.Account.Gender
}

if u.Account.Phone != "" {
data.Metadata.Phone = u.Account.Phone
data.Metadata.PhoneVerified = true
}

//
if u.Account.BirthDay != "" {
if u.Account.BirthYear == "" {
u.Account.BirthYear = "0000"
}
// format: YYYYMMDD
data.Metadata.Birthdate = u.Account.BirthYear + u.Account.BirthDay
}

return data, nil
}

Expand Down
Loading