@@ -3,6 +3,7 @@ import DefaultHandler, { order } from './default';
33import { Asset , Assets } from '../../../types' ;
44import { paginate } from '../client' ;
55import log from '../../../logger' ;
6+ import { Action } from './actions' ;
67
78// Define TokenExchangeProfile type
89export type TokenExchangeProfile = Management . TokenExchangeProfileResponseContent ;
@@ -16,10 +17,6 @@ export const schema = {
1617 type : 'string' ,
1718 description : 'The name of the token exchange profile' ,
1819 } ,
19- id : {
20- type : 'string' ,
21- description : 'The unique identifier of the token exchange profile' ,
22- } ,
2320 subject_token_type : {
2421 type : 'string' ,
2522 description : 'The URI representing the subject token type' ,
@@ -33,16 +30,6 @@ export const schema = {
3330 enum : [ 'custom_authentication' ] ,
3431 description : 'The type of token exchange profile' ,
3532 } ,
36- created_at : {
37- type : 'string' ,
38- format : 'date-time' ,
39- description : 'The timestamp when the profile was created' ,
40- } ,
41- updated_at : {
42- type : 'string' ,
43- format : 'date-time' ,
44- description : 'The timestamp when the profile was last updated' ,
45- } ,
4633 } ,
4734 required : [ 'name' , 'subject_token_type' , 'action' , 'type' ] ,
4835 } ,
@@ -51,21 +38,24 @@ export const schema = {
5138export default class TokenExchangeProfilesHandler extends DefaultHandler {
5239 existing : TokenExchangeProfile [ ] ;
5340
54- private actions : Asset [ ] | null ;
41+ private actions : Action [ ] | null ;
5542
5643 constructor ( config : DefaultHandler ) {
5744 super ( {
5845 ...config ,
5946 type : 'tokenExchangeProfiles' ,
6047 id : 'id' ,
61- identifiers : [ 'id' , 'name ' ] ,
48+ identifiers : [ 'id' , 'subject_token_type ' ] ,
6249 // Only name and subject_token_type can be updated
63- stripUpdateFields : [ 'id' , ' created_at', 'updated_at' , 'action_id' , 'type' ] ,
64- stripCreateFields : [ 'id' , ' created_at', 'updated_at' ] ,
50+ stripUpdateFields : [ 'created_at' , 'updated_at' , 'action_id' , 'type' ] ,
51+ stripCreateFields : [ 'created_at' , 'updated_at' ] ,
6552 } ) ;
6653 }
6754
68- private sanitizeForExport ( profile : TokenExchangeProfile , actions : Asset [ ] ) : TokenExchangeProfile {
55+ private sanitizeForExport (
56+ profile : TokenExchangeProfile ,
57+ actions : Action [ ]
58+ ) : TokenExchangeProfile {
6959 if ( profile . action_id ) {
7060 const action = actions ?. find ( ( a ) => a . id === profile . action_id ) ;
7161 if ( action ) {
@@ -126,10 +116,12 @@ export default class TokenExchangeProfilesHandler extends DefaultHandler {
126116 ) ;
127117
128118 // Fetch all actions to map action_id to action name
129- const actions = await this . getActions ( ) ;
119+ this . actions = await this . getActions ( ) ;
130120
131121 // Map action_id to action name for each profile
132- this . existing = profiles . map ( ( profile ) => this . sanitizeForExport ( profile , actions ) ) ;
122+ this . existing = profiles . map ( ( profile ) =>
123+ this . sanitizeForExport ( profile , this . actions ?? [ ] )
124+ ) ;
133125
134126 return this . existing ;
135127 } catch ( err ) {
@@ -150,46 +142,53 @@ export default class TokenExchangeProfilesHandler extends DefaultHandler {
150142 // Do nothing if not set
151143 if ( ! tokenExchangeProfiles ) return ;
152144
153- // Fetch actions to resolve action names to IDs
154- const actions = await this . getActions ( ) ;
155-
156- // Map action names to action_ids before processing
157- const sanitizedProfiles = tokenExchangeProfiles . map ( ( profile ) =>
158- this . sanitizeForAPI ( profile as TokenExchangeProfile , actions )
159- ) ;
160-
161- // Create modified assets with sanitized profiles
162- const modifiedAssets = {
163- ...assets ,
164- tokenExchangeProfiles : sanitizedProfiles as TokenExchangeProfile [ ] ,
165- } ;
166-
167145 // Calculate changes
168- const { del, update, create, conflicts } = await this . calcChanges ( modifiedAssets ) ;
146+ const { del, update, create, conflicts } = await this . calcChanges ( assets ) ;
169147
170148 log . debug (
171149 `Start processChanges for tokenExchangeProfiles [delete:${ del . length } ] [update:${ update . length } ], [create:${ create . length } ], [conflicts:${ conflicts . length } ]`
172150 ) ;
173151
152+ // Fetch actions to resolve action names to IDs
153+ if ( ! this . actions || this . actions . length === 0 ) {
154+ this . actions = await this . getActions ( ) ;
155+ }
156+
174157 // Process changes in order: delete, create, update
175158 if ( del . length > 0 ) {
176- await this . deleteTokenExchangeProfiles ( del ) ;
159+ await this . deleteTokenExchangeProfiles (
160+ del . map ( ( profile ) => this . sanitizeForAPI ( profile , this . actions ?? [ ] ) )
161+ ) ;
177162 }
178163
179164 if ( create . length > 0 ) {
180- await this . createTokenExchangeProfiles ( create ) ;
165+ await this . createTokenExchangeProfiles (
166+ create . map ( ( profile ) => this . sanitizeForAPI ( profile , this . actions ?? [ ] ) )
167+ ) ;
181168 }
182169
183170 if ( update . length > 0 ) {
184- await this . updateTokenExchangeProfiles ( update ) ;
171+ await this . updateTokenExchangeProfiles (
172+ update . map ( ( profile ) => this . sanitizeForAPI ( profile , this . actions ?? [ ] ) )
173+ ) ;
185174 }
186175 }
187176
188177 async createTokenExchangeProfile ( profile : TokenExchangeProfile ) : Promise < TokenExchangeProfile > {
189- const { id, created_at, updated_at, ...createParams } = profile ;
190- const created = await this . client . tokenExchangeProfiles . create (
191- createParams as Management . CreateTokenExchangeProfileRequestContent
192- ) ;
178+ if ( ! profile . name || ! profile . subject_token_type || ! profile . action_id || ! profile . type ) {
179+ throw new Error ( `Cannot create token exchange profile missing required fields` ) ;
180+ }
181+
182+ const createParams : Management . CreateTokenExchangeProfileRequestContent & {
183+ type : Management . TokenExchangeProfileTypeEnum ;
184+ } = {
185+ name : profile . name ,
186+ subject_token_type : profile . subject_token_type ,
187+ action_id : profile . action_id ,
188+ type : profile . type ,
189+ } ;
190+
191+ const created = await this . client . tokenExchangeProfiles . create ( createParams ) ;
193192 return created ;
194193 }
195194
@@ -211,12 +210,17 @@ export default class TokenExchangeProfilesHandler extends DefaultHandler {
211210 }
212211
213212 async updateTokenExchangeProfile ( profile : TokenExchangeProfile ) : Promise < void > {
214- const { id, created_at , updated_at , action_id , type , ... updateParams } = profile ;
213+ const { id, name , subject_token_type } = profile ;
215214
216215 if ( ! id ) {
217216 throw new Error ( `Cannot update token exchange profile "${ profile . name } " - missing id` ) ;
218217 }
219218
219+ const updateParams : Management . UpdateTokenExchangeProfileRequestContent = {
220+ name,
221+ subject_token_type,
222+ } ;
223+
220224 await this . client . tokenExchangeProfiles . update ( id , updateParams ) ;
221225 }
222226
0 commit comments