Skip to content

Commit f428bb1

Browse files
authored
feat(auth): Extract more information from OIDC token (#2097)
Signed-off-by: Javier Rodriguez <[email protected]>
1 parent b046773 commit f428bb1

31 files changed

+992
-212
lines changed

app/cli/cmd/organization_describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newOrganizationDescribeCmd() *cobra.Command {
4444
func contextTableOutput(config *action.ConfigContextItem) error {
4545
gt := newTableWriter()
4646
gt.SetTitle("Current Context")
47-
gt.AppendRow(table.Row{"Logged in as", config.CurrentUser.Email})
47+
gt.AppendRow(table.Row{"Logged in as", config.CurrentUser.PrintUserProfileWithEmail()})
4848
gt.AppendSeparator()
4949

5050
if m := config.CurrentMembership; m != nil {

app/cli/cmd/organization_member_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func orgMembershipsTableOutput(items []*action.MembershipItem) error {
5252
t.AppendHeader(table.Row{"ID", "Email", "Role", "Joined At"})
5353

5454
for _, i := range items {
55-
t.AppendRow(table.Row{i.ID, i.User.Email, i.Role, i.CreatedAt.Format(time.RFC822)})
55+
t.AppendRow(table.Row{i.ID, i.User.PrintUserProfileWithEmail(), i.Role, i.CreatedAt.Format(time.RFC822)})
5656
t.AppendSeparator()
5757
}
5858

app/cli/internal/action/config_current_context.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,29 @@ type ConfigContextItem struct {
3737
}
3838

3939
type UserItem struct {
40-
ID, Email string
41-
CreatedAt *time.Time
40+
ID, Email, FirstName, LastName string
41+
CreatedAt *time.Time
42+
}
43+
44+
// PrintUserProfileWithEmail formats the user's profile with their email.
45+
func (u *UserItem) PrintUserProfileWithEmail() string {
46+
var name string
47+
48+
// Build name based on available parts
49+
switch {
50+
case u.FirstName != "" && u.LastName != "":
51+
name = u.FirstName + " " + u.LastName
52+
case u.FirstName != "":
53+
name = u.FirstName
54+
case u.LastName != "":
55+
name = u.LastName
56+
}
57+
58+
// If we have a name, format with email, otherwise just return email
59+
if name != "" {
60+
return name + " <" + u.Email + ">"
61+
}
62+
return u.Email
4263
}
4364

4465
func (action *ConfigCurrentContext) Run() (*ConfigContextItem, error) {
@@ -65,6 +86,8 @@ func pbUserItemToAction(in *pb.User) *UserItem {
6586
return &UserItem{
6687
ID: in.Id,
6788
Email: in.Email,
89+
FirstName: in.FirstName,
90+
LastName: in.LastName,
6891
CreatedAt: toTimePtr(in.CreatedAt.AsTime()),
6992
}
7093
}

app/controlplane/api/controlplane/v1/response_messages.pb.go

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

app/controlplane/api/controlplane/v1/response_messages.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ message User {
222222
string id = 1;
223223
string email = 2;
224224
google.protobuf.Timestamp created_at = 3;
225+
string first_name = 4;
226+
string last_name = 5;
225227
}
226228

227229
message OrgMembershipItem {

app/controlplane/api/gen/frontend/controlplane/v1/response_messages.ts

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/gen/jsonschema/controlplane.v1.User.jsonschema.json

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

app/controlplane/api/gen/jsonschema/controlplane.v1.User.schema.json

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

app/controlplane/internal/service/auth.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ type upstreamOIDCclaims struct {
260260
// https://learn.microsoft.com/en-us/entra/identity/authentication/howto-authentication-use-email-signin
261261
// https://learn.microsoft.com/en-us/entra/identity-platform/id-token-claims-reference
262262
PreferredUsername string `json:"preferred_username"`
263+
FamilyName string `json:"family_name"`
264+
GivenName string `json:"given_name"`
263265
}
264266

265267
// Will retrieve the email from the preferred username if it's a valid email
@@ -304,7 +306,10 @@ func callbackHandler(svc *AuthService, w http.ResponseWriter, r *http.Request) *
304306
}
305307

306308
// Create user if needed
307-
u, err := svc.userUseCase.FindOrCreateByEmail(ctx, claims.preferredEmail())
309+
u, err := svc.userUseCase.UpsertByEmail(ctx, claims.preferredEmail(), &biz.UpsertByEmailOpts{
310+
FirstName: &claims.GivenName,
311+
LastName: &claims.FamilyName,
312+
})
308313
if err != nil {
309314
return newOauthResp(http.StatusInternalServerError, fmt.Errorf("failed to find or create user: %w", err), false)
310315
}
@@ -432,7 +437,7 @@ func setOauthCookie(w http.ResponseWriter, name, value string) {
432437

433438
func generateAndLogDevUser(userUC *biz.UserUseCase, log *log.Helper, authConfig *conf.Auth) error {
434439
// Create user if needed
435-
u, err := userUC.FindOrCreateByEmail(context.Background(), authConfig.DevUser)
440+
u, err := userUC.UpsertByEmail(context.Background(), authConfig.DevUser, nil)
436441
if err != nil {
437442
return sl.LogAndMaskErr(err, log)
438443
}

app/controlplane/internal/service/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *ContextService) Current(ctx context.Context, _ *pb.ContextServiceCurren
6767
}
6868
} else if currentUser != nil {
6969
res.CurrentUser = &pb.User{
70-
Id: currentUser.ID, Email: currentUser.Email, CreatedAt: timestamppb.New(*currentUser.CreatedAt),
70+
Id: currentUser.ID, Email: currentUser.Email, FirstName: currentUser.FirstName, LastName: currentUser.LastName, CreatedAt: timestamppb.New(*currentUser.CreatedAt),
7171
}
7272

7373
// For regular users, we need to load the membership manually
@@ -121,7 +121,7 @@ func bizOrgToPb(m *biz.Organization) *pb.OrgItem {
121121
}
122122

123123
func bizUserToPb(u *biz.User) *pb.User {
124-
return &pb.User{Id: u.ID, Email: u.Email, CreatedAt: timestamppb.New(*u.CreatedAt)}
124+
return &pb.User{Id: u.ID, Email: u.Email, CreatedAt: timestamppb.New(*u.CreatedAt), FirstName: u.FirstName, LastName: u.LastName}
125125
}
126126

127127
func bizPolicyViolationBlockingStrategyToPb(blockOnPolicyViolation bool) pb.OrgItem_PolicyViolationBlockingStrategy {

0 commit comments

Comments
 (0)