Skip to content

Commit abd223d

Browse files
author
symantec
committed
fix: accept current global userinfo shape in PAT login
The global /api/v1/userinfo endpoint now returns id/name/username/avatar and camelCase organization fields instead of uid/organization_tags. PAT exchange succeeded but the UID lookup failed, aborting startup with "could not fetch user info after exchange". Normalize both legacy and current response shapes, mirroring the official CLI field precedence, and include the underlying userinfo error in the failure message so future shape changes remain diagnosable.
1 parent 506605b commit abd223d

3 files changed

Lines changed: 257 additions & 10 deletions

File tree

‎auth.go‎

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,94 @@ func (a *authManager) deviceLogin(ctx context.Context, out io.Writer) error {
11231123
}
11241124
}
11251125

1126+
// authUserInfoProfile mirrors the official CLI's tolerant OpenAPI userinfo
1127+
// decoder. The endpoint has returned several shapes over time: older responses
1128+
// use uid/organization_id/organization_tags, while current responses use
1129+
// id/username/avatar/organization and camelCase organization fields. Keep the
1130+
// raw variants here and canonicalize once in normalize so callers only deal
1131+
// with the stable userInfo field names.
11261132
type authUserInfoProfile struct {
1127-
DataPolicyAgreed *bool `json:"data_policy_agreed"`
1128-
IsDataPolicyModifiable *bool `json:"is_data_policy_modifiable"`
1129-
UID string `json:"uid"`
1130-
Name string `json:"name"`
1131-
Email string `json:"email"`
1132-
OrgID string `json:"organization_id"`
1133-
OrgName string `json:"organization_name"`
1134-
OrgTags []string `json:"organization_tags"`
1133+
DataPolicyAgreed *bool `json:"data_policy_agreed"`
1134+
IsDataPolicyModifiable *bool `json:"is_data_policy_modifiable"`
1135+
IsPrivacyPolicyModifiable *bool `json:"is_privacy_policy_modifiable"`
1136+
UID string `json:"uid"`
1137+
ID string `json:"id"`
1138+
UserID string `json:"user_id"`
1139+
Name string `json:"name"`
1140+
UserName string `json:"username"`
1141+
UserNameSnake string `json:"user_name"`
1142+
Email string `json:"email"`
1143+
AvatarURL string `json:"avatar_url"`
1144+
Avatar string `json:"avatar"`
1145+
OrgID string `json:"organization_id"`
1146+
OrgIDCamel string `json:"orgId"`
1147+
OrgIDCamelAlt string `json:"organizationId"`
1148+
OrgName string `json:"organization_name"`
1149+
OrgNameCamel string `json:"orgName"`
1150+
OrgNameCamelAlt string `json:"organizationName"`
1151+
Organization *authUserInfoOrganization `json:"organization"`
1152+
OrgTags []string `json:"organization_tags"`
1153+
}
1154+
1155+
type authUserInfoOrganization struct {
1156+
ID string `json:"id"`
1157+
UID string `json:"uid"`
1158+
OrgID string `json:"org_id"`
1159+
OrgIDCamel string `json:"orgId"`
1160+
OrganizationID string `json:"organization_id"`
1161+
Name string `json:"name"`
1162+
OrgName string `json:"org_name"`
1163+
OrgNameCamel string `json:"orgName"`
1164+
OrganizationName string `json:"organization_name"`
1165+
Tags []string `json:"tags"`
1166+
OrganizationTags []string `json:"organization_tags"`
1167+
}
1168+
1169+
func firstNonEmptyString(values ...string) string {
1170+
for _, value := range values {
1171+
if value != "" {
1172+
return value
1173+
}
1174+
}
1175+
return ""
1176+
}
1177+
1178+
func firstNonEmptyStrings(values ...[]string) []string {
1179+
for _, value := range values {
1180+
if value != nil {
1181+
return value
1182+
}
1183+
}
1184+
return nil
1185+
}
1186+
1187+
// normalize canonicalizes the alternate wire shapes into the fields consumed
1188+
// by applyAuthUserInfoProfile and requestPATCandidate. Precedence follows the
1189+
// official CLI: id, user_id, uid for identity; name, username, user_name for
1190+
// the display name; camelCase then snake_case organization fields; and the
1191+
// nested organization object as the final fallback.
1192+
func (p *authUserInfoProfile) normalize() {
1193+
if p == nil {
1194+
return
1195+
}
1196+
p.UID = firstNonEmptyString(p.ID, p.UserID, p.UID)
1197+
p.Name = firstNonEmptyString(p.Name, p.UserName, p.UserNameSnake)
1198+
p.AvatarURL = firstNonEmptyString(p.AvatarURL, p.Avatar)
1199+
if p.IsDataPolicyModifiable == nil {
1200+
p.IsDataPolicyModifiable = p.IsPrivacyPolicyModifiable
1201+
}
1202+
orgID := firstNonEmptyString(p.OrgIDCamel, p.OrgID, p.OrgIDCamelAlt)
1203+
orgName := firstNonEmptyString(p.OrgNameCamel, p.OrgName, p.OrgNameCamelAlt)
1204+
tags := p.OrgTags
1205+
if org := p.Organization; org != nil {
1206+
orgID = firstNonEmptyString(orgID, org.OrgID, org.OrgIDCamel, org.OrganizationID, org.ID, org.UID)
1207+
orgName = firstNonEmptyString(orgName, org.OrgName, org.OrgNameCamel, org.OrganizationName, org.Name)
1208+
tags = firstNonEmptyStrings(tags, org.Tags, org.OrganizationTags)
1209+
}
1210+
p.OrgID, p.OrgName = orgID, orgName
1211+
if tags != nil {
1212+
p.OrgTags = tags
1213+
}
11351214
}
11361215

11371216
func (a *authManager) requestUserInfo(ctx context.Context, token string, held ...*credentialTxn) (authUserInfoProfile, error) {
@@ -1176,6 +1255,7 @@ func (a *authManager) requestUserInfo(ctx context.Context, token string, held ..
11761255
if err := json.Unmarshal(raw, &profile); err != nil {
11771256
return authUserInfoProfile{}, err
11781257
}
1258+
profile.normalize()
11791259
return profile, nil
11801260
}
11811261

@@ -1198,6 +1278,9 @@ func applyAuthUserInfoProfile(ui *userInfo, profile authUserInfoProfile) {
11981278
if profile.Name != "" {
11991279
ui.Name = profile.Name
12001280
}
1281+
if profile.AvatarURL != "" {
1282+
ui.AvatarURL = profile.AvatarURL
1283+
}
12011284
if profile.OrgID != "" {
12021285
ui.OrgID, ui.OrgName = profile.OrgID, profile.OrgName
12031286
}

‎auth_userinfo_shape_test.go‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
func TestAuthUserInfoProfileNormalizesCurrentGlobalShape(t *testing.T) {
12+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
if r.URL.Path != "/api/v1/userinfo" {
14+
http.NotFound(w, r)
15+
return
16+
}
17+
w.Header().Set("Content-Type", "application/json")
18+
_, _ = w.Write([]byte(`{
19+
"id":"synthetic-openapi-id",
20+
"name":"Synthetic Display",
21+
"nick_name":"Synthetic Nick",
22+
"username":"synthetic-login",
23+
"avatar":"https://qoder.example/avatar",
24+
"email":"synthetic@example.invalid",
25+
"organization_id":"synthetic-org",
26+
"organization_name":"Synthetic Org",
27+
"is_privacy_policy_modifiable":false
28+
}`))
29+
}))
30+
defer server.Close()
31+
32+
manager := &authManager{
33+
openapiBase: server.URL,
34+
httpc: server.Client(),
35+
logf: func(string, ...any) {},
36+
}
37+
profile, err := manager.requestUserInfo(context.Background(), "synthetic-token")
38+
if err != nil {
39+
t.Fatalf("requestUserInfo() error = %v", err)
40+
}
41+
if profile.UID != "synthetic-openapi-id" {
42+
t.Fatalf("profile UID = %q, want current-shape id", profile.UID)
43+
}
44+
if profile.Name != "Synthetic Display" {
45+
t.Fatalf("profile Name = %q, want name", profile.Name)
46+
}
47+
if profile.OrgID != "synthetic-org" || profile.OrgName != "Synthetic Org" {
48+
t.Fatalf("profile organization = %q/%q, want synthetic-org/Synthetic Org", profile.OrgID, profile.OrgName)
49+
}
50+
if profile.AvatarURL != "https://qoder.example/avatar" {
51+
t.Fatalf("profile AvatarURL = %q, want avatar fallback", profile.AvatarURL)
52+
}
53+
if profile.IsDataPolicyModifiable == nil || *profile.IsDataPolicyModifiable {
54+
t.Fatal("profile privacy-policy flag was not normalized to false")
55+
}
56+
57+
ui := &userInfo{}
58+
applyAuthUserInfoProfile(ui, profile)
59+
if ui.UID != "synthetic-openapi-id" || ui.OrgID != "synthetic-org" || ui.AvatarURL != "https://qoder.example/avatar" {
60+
t.Fatalf("applied profile = %+v", ui)
61+
}
62+
if ui.IsDataPolicyModifiable {
63+
t.Fatal("applied privacy-policy flag = true, want false")
64+
}
65+
}
66+
67+
func TestAuthUserInfoProfileKeepsLegacyShape(t *testing.T) {
68+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69+
if r.URL.Path != "/api/v1/userinfo" {
70+
http.NotFound(w, r)
71+
return
72+
}
73+
w.Header().Set("Content-Type", "application/json")
74+
_, _ = w.Write([]byte(`{
75+
"uid":"synthetic-legacy-uid",
76+
"name":"Legacy Name",
77+
"email":"legacy@example.invalid",
78+
"avatar_url":"https://qoder.example/legacy-avatar",
79+
"organization_id":"legacy-org",
80+
"organization_name":"Legacy Org",
81+
"organization_tags":["legacy-tag"],
82+
"data_policy_agreed":true,
83+
"is_data_policy_modifiable":true
84+
}`))
85+
}))
86+
defer server.Close()
87+
88+
manager := &authManager{
89+
openapiBase: server.URL,
90+
httpc: server.Client(),
91+
logf: func(string, ...any) {},
92+
}
93+
profile, err := manager.requestUserInfo(context.Background(), "synthetic-token")
94+
if err != nil {
95+
t.Fatalf("requestUserInfo() error = %v", err)
96+
}
97+
if profile.UID != "synthetic-legacy-uid" || profile.OrgID != "legacy-org" {
98+
t.Fatalf("legacy profile = %+v", profile)
99+
}
100+
if profile.DataPolicyAgreed == nil || !*profile.DataPolicyAgreed {
101+
t.Fatal("legacy data_policy_agreed was not decoded")
102+
}
103+
if profile.IsDataPolicyModifiable == nil || !*profile.IsDataPolicyModifiable {
104+
t.Fatal("legacy is_data_policy_modifiable was not decoded")
105+
}
106+
if len(profile.OrgTags) != 1 || profile.OrgTags[0] != "legacy-tag" {
107+
t.Fatalf("legacy organization tags = %v", profile.OrgTags)
108+
}
109+
}
110+
111+
func TestAuthManagerPATLoginAcceptsCurrentGlobalUserInfoShape(t *testing.T) {
112+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113+
w.Header().Set("Content-Type", "application/json")
114+
switch r.URL.Path {
115+
case "/api/v1/jobToken/exchange":
116+
_, _ = w.Write([]byte(`{"token":"synthetic-current-pat-token","refresh_token":"synthetic-current-pat-refresh","expires_in":7200}`))
117+
case "/api/v1/userinfo":
118+
_, _ = w.Write([]byte(`{
119+
"id":"synthetic-current-pat-user",
120+
"name":"Synthetic Current User",
121+
"username":"synthetic-current-login",
122+
"avatar":"https://qoder.example/current-avatar",
123+
"organization_id":"synthetic-current-org",
124+
"organization_name":"Synthetic Current Org",
125+
"is_privacy_policy_modifiable":false
126+
}`))
127+
default:
128+
http.NotFound(w, r)
129+
}
130+
}))
131+
defer server.Close()
132+
133+
generator := &fakeRuntimeFieldGenerator{output: runtimeFieldOutput{EncryptUserInfo: "synthetic-runtime-field", Key: "synthetic-runtime-key"}}
134+
factory := &fakeProtocolContextFactory{contexts: []protocolContext{&fakeProtocolContext{}}}
135+
codec := &fakeCredentialCodec{encrypted: "synthetic-encrypted-credential"}
136+
manager := syntheticAuthManager(t, &authManager{
137+
protocol: &protocolServices{
138+
credentials: codec,
139+
runtimeFields: generator,
140+
contextFactory: factory,
141+
},
142+
authFile: filepath.Join(t.TempDir(), "user"),
143+
machineID: "0123456789abcdef-extra",
144+
openapiBase: server.URL,
145+
httpc: server.Client(),
146+
logf: func(string, ...any) {},
147+
})
148+
149+
if err := patLogin(context.Background(), manager, "synthetic-pat"); err != nil {
150+
t.Fatalf("patLogin() error = %v", err)
151+
}
152+
if manager.ui == nil {
153+
t.Fatal("PAT login did not commit a user profile")
154+
}
155+
if manager.ui.UID != "synthetic-current-pat-user" {
156+
t.Fatalf("committed UID = %q, want current-shape id", manager.ui.UID)
157+
}
158+
if manager.ui.Name != "Synthetic Current User" || manager.ui.OrgID != "synthetic-current-org" {
159+
t.Fatalf("committed profile = %+v", manager.ui)
160+
}
161+
if generator.calls != 1 || len(factory.configs) != 1 {
162+
t.Fatalf("runtime field/context calls = %d/%d, want 1/1", generator.calls, len(factory.configs))
163+
}
164+
}

‎main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,10 @@ func (am *authManager) requestPATCandidate(ctx context.Context, pat string, prev
438438
if isAuthConflict(err) {
439439
return nil, err
440440
}
441-
return nil, fmt.Errorf("could not fetch user info after exchange")
441+
return nil, fmt.Errorf("could not fetch user info after exchange: %w", err)
442442
}
443443
if profile.UID == "" {
444-
return nil, fmt.Errorf("could not fetch user info after exchange")
444+
return nil, fmt.Errorf("could not fetch user info after exchange: userinfo response missing user id")
445445
}
446446
applyAuthUserInfoProfile(candidate, profile)
447447
return candidate, nil

0 commit comments

Comments
 (0)