Skip to content

Commit 4a39625

Browse files
committed
chore: implement user link claims as a typed golang object
Move claims from a `debug` column to an actual typed column to be used.
1 parent 55dccae commit 4a39625

File tree

8 files changed

+70
-39
lines changed

8 files changed

+70
-39
lines changed

coderd/database/dump.sql

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE user_links RENAME COLUMN claims TO debug_context;
2+
3+
COMMENT ON COLUMN user_links.debug_context IS 'Debug information includes information like id_token and userinfo claims.';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE user_links RENAME COLUMN debug_context TO claims;
2+
3+
COMMENT ON COLUMN user_links.claims IS 'Claims from the IDP for the linked user. Includes both id_token and userinfo claims. ';

coderd/database/models.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/user_links.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ INSERT INTO
3232
oauth_refresh_token,
3333
oauth_refresh_token_key_id,
3434
oauth_expiry,
35-
debug_context
35+
claims
3636
)
3737
VALUES
3838
( $1, $2, $3, $4, $5, $6, $7, $8, $9 ) RETURNING *;
@@ -54,6 +54,6 @@ SET
5454
oauth_refresh_token = $3,
5555
oauth_refresh_token_key_id = $4,
5656
oauth_expiry = $5,
57-
debug_context = $6
57+
claims = $6
5858
WHERE
5959
user_id = $7 AND login_type = $8 RETURNING *;

coderd/database/sqlc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ sql:
7979
- column: "provisioner_job_stats.*_secs"
8080
go_type:
8181
type: "float64"
82+
- column: "user_links.claims"
83+
go_type:
84+
type: "UserLinkClaims"
8285
rename:
8386
group_member: GroupMemberTable
8487
group_members_expanded: GroupMember

coderd/database/types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,25 @@ func (p *AgentIDNamePair) Scan(src interface{}) error {
207207
func (p AgentIDNamePair) Value() (driver.Value, error) {
208208
return fmt.Sprintf(`(%s,%s)`, p.ID.String(), p.Name), nil
209209
}
210+
211+
// UserLinkClaims is the returned IDP claims for a given user link.
212+
// These claims are fetched at login time. These are the claims that were
213+
// used for IDP sync.
214+
type UserLinkClaims struct {
215+
IDTokenClaims map[string]interface{} `json:"id_token_claims"`
216+
UserInfoClaims map[string]interface{} `json:"user_info_claims"`
217+
}
218+
219+
func (a *UserLinkClaims) Scan(src interface{}) error {
220+
switch v := src.(type) {
221+
case string:
222+
return json.Unmarshal([]byte(v), &a)
223+
case []byte:
224+
return json.Unmarshal(v, &a)
225+
}
226+
return xerrors.Errorf("unexpected type %T", src)
227+
}
228+
229+
func (a *UserLinkClaims) Value() (driver.Value, error) {
230+
return json.Marshal(a)
231+
}

0 commit comments

Comments
 (0)