Skip to content

Commit bee1e57

Browse files
committed
Add GetOrCreateUserInfoByGitHubID to manage users by GitHub (#1193)
This commit enhanced authentication by integrating GitHub OAuth support. Users can now sign in with GitHub, and profiles display the selected authentication provider.
1 parent 3cc4175 commit bee1e57

File tree

17 files changed

+517
-257
lines changed

17 files changed

+517
-257
lines changed

api/converter/from_pb.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ import (
3333
// FromUser converts the given Protobuf formats to model format.
3434
func FromUser(pbUser *api.User) *types.User {
3535
return &types.User{
36-
ID: types.ID(pbUser.Id),
37-
Username: pbUser.Username,
38-
CreatedAt: pbUser.CreatedAt.AsTime(),
36+
ID: types.ID(pbUser.Id),
37+
AuthProvider: pbUser.AuthProvider,
38+
Username: pbUser.Username,
39+
CreatedAt: pbUser.CreatedAt.AsTime(),
3940
}
4041
}
4142

api/converter/to_pb.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import (
3636
// ToUser converts the given model format to Protobuf format.
3737
func ToUser(user *types.User) *api.User {
3838
return &api.User{
39-
Id: user.ID.String(),
40-
Username: user.Username,
41-
CreatedAt: timestamppb.New(user.CreatedAt),
39+
Id: user.ID.String(),
40+
AuthProvider: user.AuthProvider,
41+
Username: user.Username,
42+
CreatedAt: timestamppb.New(user.CreatedAt),
4243
}
4344
}
4445

api/docs/yorkie/v1/admin.openapi.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ components:
21872187
title: max_attachments_per_document
21882188
type: object
21892189
maxSubscribersPerDocument:
2190-
$ref: "#/components/schemas/google.protobuf.Int32Value"
2190+
$ref: '#/components/schemas/google.protobuf.Int32Value'
21912191
additionalProperties: false
21922192
description: ""
21932193
title: max_subscribers_per_document
@@ -2259,6 +2259,11 @@ components:
22592259
additionalProperties: false
22602260
description: ""
22612261
properties:
2262+
authProvider:
2263+
additionalProperties: false
2264+
description: ""
2265+
title: auth_provider
2266+
type: string
22622267
createdAt:
22632268
$ref: '#/components/schemas/google.protobuf.Timestamp'
22642269
additionalProperties: false

api/docs/yorkie/v1/resources.openapi.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ components:
17421742
title: max_attachments_per_document
17431743
type: object
17441744
maxSubscribersPerDocument:
1745-
$ref: "#/components/schemas/google.protobuf.Int32Value"
1745+
$ref: '#/components/schemas/google.protobuf.Int32Value'
17461746
additionalProperties: false
17471747
description: ""
17481748
title: max_subscribers_per_document
@@ -1785,6 +1785,11 @@ components:
17851785
additionalProperties: false
17861786
description: ""
17871787
properties:
1788+
authProvider:
1789+
additionalProperties: false
1790+
description: ""
1791+
title: auth_provider
1792+
type: string
17881793
createdAt:
17891794
$ref: '#/components/schemas/google.protobuf.Timestamp'
17901795
additionalProperties: false

api/types/user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type User struct {
2424
// ID is the unique ID of the user.
2525
ID ID `json:"id"`
2626

27+
// AuthProvider is the authentication provider of the user. It can be
28+
// "github" or empty string for local authentication.
29+
AuthProvider string `json:"auth_provider"`
30+
2731
// Username is the username of the user.
2832
Username string `json:"username"`
2933

api/yorkie/v1/resources.pb.go

Lines changed: 215 additions & 205 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/yorkie/v1/resources.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ message TreePos {
286286

287287
message User {
288288
string id = 1;
289-
string username = 2;
290-
google.protobuf.Timestamp created_at = 3;
289+
string auth_provider = 2;
290+
string username = 3;
291+
google.protobuf.Timestamp created_at = 4;
291292
}
292293

293294
message Project {

build/charts/yorkie-cluster/templates/yorkie/deployment.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@ spec:
9292
"--kafka-write-timeout",
9393
"{{ .Values.yorkie.args.kafkaWriteTimeout }}",
9494
{{- end }}
95+
{{- if .Values.yorkie.args.GitHubClientID }}
96+
"--auth-github-client-id",
97+
"{{ .Values.yorkie.args.GitHubClientID }}",
98+
{{- end }}
99+
{{- if .Values.yorkie.args.GitHubClientSecret }}
100+
"--auth-github-client-secret",
101+
"{{ .Values.yorkie.args.GitHubClientSecret }}",
102+
{{- end }}
103+
{{- if .Values.yorkie.args.GitHubRedirectURL }}
104+
"--auth-github-redirect-url",
105+
"{{ .Values.yorkie.args.GitHubRedirectURL }}",
106+
{{- end }}
107+
{{- if .Values.yorkie.args.GitHubAuthURL }}
108+
"--auth-github-auth-url",
109+
"{{ .Values.yorkie.args.GitHubAuthURL }}",
110+
{{- end }}
111+
{{- if .Values.yorkie.args.GitHubTokenURL }}
112+
"--auth-github-token-url",
113+
"{{ .Values.yorkie.args.GitHubTokenURL }}",
114+
{{- end }}
115+
{{- if .Values.yorkie.args.GitHubUserURL }}
116+
"--auth-github-user-url",
117+
"{{ .Values.yorkie.args.GitHubUserURL }}",
118+
{{- end }}
119+
{{- if .Values.yorkie.args.GitHubDeviceAuthURL }}
120+
"--auth-github-device-auth-url",
121+
"{{ .Values.yorkie.args.GitHubDeviceAuthURL }}",
122+
{{- end }}
95123
{{- end }}
96124
]
97125
ports:

cmd/yorkie/login.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func newLoginCmd() *cobra.Command {
5353
cli.Close()
5454
}()
5555

56+
// TODO(hackerwins): For now, the login command only supports the
57+
// username/password login. We need to support other login methods
58+
// like GitHub.
5659
ctx := context.Background()
5760
token, err := cli.LogIn(ctx, username, password)
5861
if err != nil {

cmd/yorkie/server.go

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ var (
3939
flagConfPath string
4040
flagLogLevel string
4141

42-
adminTokenDuration time.Duration
42+
adminTokenDuration time.Duration
43+
authGitHubClientID string
44+
authGitHubClientSecret string
45+
authGitHubRedirectURL string
46+
authGitHubUserURL string
47+
authGitHubAuthURL string
48+
authGitHubTokenURL string
49+
authGitHubDeviceAuthURL string
50+
4351
housekeepingInterval time.Duration
4452
clientDeactivateThreshold string
4553

@@ -72,6 +80,14 @@ func newServerCmd() *cobra.Command {
7280
Use: "server [options]",
7381
Short: "Start Yorkie server",
7482
RunE: func(cmd *cobra.Command, args []string) error {
83+
conf.RPC.Auth.GitHubClientID = authGitHubClientID
84+
conf.RPC.Auth.GitHubClientSecret = authGitHubClientSecret
85+
conf.RPC.Auth.GitHubRedirectURL = authGitHubRedirectURL
86+
conf.RPC.Auth.GitHubUserURL = authGitHubUserURL
87+
conf.RPC.Auth.GitHubAuthURL = authGitHubAuthURL
88+
conf.RPC.Auth.GitHubTokenURL = authGitHubTokenURL
89+
conf.RPC.Auth.GitHubDeviceAuthURL = authGitHubDeviceAuthURL
90+
7591
conf.Backend.AdminTokenDuration = adminTokenDuration.String()
7692

7793
conf.Backend.ClientDeactivateThreshold = clientDeactivateThreshold
@@ -219,6 +235,56 @@ func init() {
219235
false,
220236
"Enable runtime profiling data via HTTP server.",
221237
)
238+
239+
cmd.Flags().DurationVar(
240+
&adminTokenDuration,
241+
"backend-admin-token-duration",
242+
server.DefaultAdminTokenDuration,
243+
"The duration of the admin authentication token.",
244+
)
245+
cmd.Flags().StringVar(
246+
&authGitHubClientID,
247+
"auth-github-client-id",
248+
"",
249+
"GitHub OAuth Client ID",
250+
)
251+
cmd.Flags().StringVar(
252+
&authGitHubClientSecret,
253+
"auth-github-client-secret",
254+
"",
255+
"GitHub OAuth Client Secret",
256+
)
257+
cmd.Flags().StringVar(
258+
&authGitHubRedirectURL,
259+
"auth-github-redirect-url",
260+
"http://localhost:8080/auth/github/callback",
261+
"GitHub OAuth callback URL",
262+
)
263+
cmd.Flags().StringVar(
264+
&authGitHubUserURL,
265+
"auth-github-user-url",
266+
server.DefaultGitHubUserURL,
267+
"GitHub User API URL",
268+
)
269+
cmd.Flags().StringVar(
270+
&authGitHubAuthURL,
271+
"auth-github-auth-url",
272+
server.DefaultGitHubAuthURL,
273+
"GitHub OAuth2 authorization URL",
274+
)
275+
cmd.Flags().StringVar(
276+
&authGitHubTokenURL,
277+
"auth-github-token-url",
278+
server.DefaultGitHubTokenURL,
279+
"GitHub OAuth2 token URL",
280+
)
281+
cmd.Flags().StringVar(
282+
&authGitHubDeviceAuthURL,
283+
"auth-github-device-auth-url",
284+
server.DefaultGitHubDeviceAuthURL,
285+
"GitHub OAuth2 device authorization URL",
286+
)
287+
222288
cmd.Flags().DurationVar(
223289
&housekeepingInterval,
224290
"housekeeping-interval",
@@ -279,12 +345,7 @@ func init() {
279345
server.DefaultSecretKey,
280346
"The secret key for signing authentication tokens for admin users.",
281347
)
282-
cmd.Flags().DurationVar(
283-
&adminTokenDuration,
284-
"backend-admin-token-duration",
285-
server.DefaultAdminTokenDuration,
286-
"The duration of the admin authentication token.",
287-
)
348+
288349
cmd.Flags().BoolVar(
289350
&conf.Backend.UseDefaultProject,
290351
"backend-use-default-project",

0 commit comments

Comments
 (0)