11import { FastifyPluginAsync } from "fastify" ;
2- import { AppRoles } from "../../common/roles.js" ;
2+ import { allAppRoles , AppRoles } from "../../common/roles.js" ;
33import { zodToJsonSchema } from "zod-to-json-schema" ;
44import {
55 addToTenant ,
66 getEntraIdToken ,
77 listGroupMembers ,
88 modifyGroup ,
9+ patchUserProfile ,
910} from "../functions/entraId.js" ;
1011import {
1112 BaseError ,
@@ -15,6 +16,7 @@ import {
1516 EntraInvitationError ,
1617 InternalServerError ,
1718 NotFoundError ,
19+ UnauthorizedError ,
1820} from "../../common/errors/index.js" ;
1921import { PutItemCommand } from "@aws-sdk/client-dynamodb" ;
2022import { genericConfig } from "../../common/config.js" ;
@@ -29,13 +31,48 @@ import {
2931 GroupModificationPatchRequest ,
3032 EntraGroupActions ,
3133 entraGroupMembershipListResponse ,
34+ ProfilePatchRequest ,
35+ entraProfilePatchRequest ,
3236} from "../../common/types/iam.js" ;
3337import {
3438 AUTH_DECISION_CACHE_SECONDS ,
3539 getGroupRoles ,
3640} from "../functions/authorization.js" ;
3741
3842const iamRoutes : FastifyPluginAsync = async ( fastify , _options ) => {
43+ fastify . patch < { Body : ProfilePatchRequest } > (
44+ "/profile" ,
45+ {
46+ preValidation : async ( request , reply ) => {
47+ await fastify . zodValidateBody ( request , reply , entraProfilePatchRequest ) ;
48+ } ,
49+ onRequest : async ( request , reply ) => {
50+ await fastify . authorize ( request , reply , allAppRoles ) ;
51+ } ,
52+ } ,
53+ async ( request , reply ) => {
54+ if ( ! request . tokenPayload || ! request . username ) {
55+ throw new UnauthorizedError ( {
56+ message : "User does not have the privileges for this task." ,
57+ } ) ;
58+ }
59+ const userOid = request . tokenPayload [ "oid" ] ;
60+ const entraIdToken = await getEntraIdToken (
61+ {
62+ smClient : fastify . secretsManagerClient ,
63+ dynamoClient : fastify . dynamoClient ,
64+ } ,
65+ fastify . environmentConfig . AadValidClientId ,
66+ ) ;
67+ await patchUserProfile (
68+ entraIdToken ,
69+ request . username ,
70+ userOid ,
71+ request . body ,
72+ ) ;
73+ reply . send ( 201 ) ;
74+ } ,
75+ ) ;
3976 fastify . get < {
4077 Body : undefined ;
4178 Querystring : { groupId : string } ;
0 commit comments