@@ -7,11 +7,14 @@ namespace DocuSign.CodeExamples
77{
88 using System ;
99 using System . Collections . Generic ;
10+ using System . Linq ;
1011 using System . Net . Http ;
1112 using System . Net . Http . Headers ;
1213 using System . Security . Claims ;
1314 using System . Text . Json ;
15+ using System . Text . RegularExpressions ;
1416 using System . Threading . Tasks ;
17+ using System . Web ;
1518 using DocuSign . CodeExamples . Common ;
1619 using DocuSign . CodeExamples . Models ;
1720 using DocuSign . Rooms . Api ;
@@ -35,7 +38,7 @@ public Startup(IConfiguration configuration)
3538 {
3639 this . Configuration = configuration ;
3740
38- this . apiTypes . Add ( ExamplesApiType . ESignature , new List < string > { "signature" } ) ;
41+ this . apiTypes . Add ( ExamplesApiType . ESignature , new List < string > { "signature" , } ) ;
3942
4043 this . apiTypes . Add ( ExamplesApiType . Rooms , new List < string >
4144 {
@@ -77,6 +80,11 @@ public Startup(IConfiguration configuration)
7780 "asset_group_account_clone_write" ,
7881 "asset_group_account_clone_read" ,
7982 } ) ;
83+
84+ this . apiTypes . Add ( ExamplesApiType . WebForms , new List < string >
85+ {
86+ "signature" , "webforms_read" , "webforms_instance_write" , "webforms_instance_read" ,
87+ } ) ;
8088 }
8189
8290 public IConfiguration Configuration { get ; }
@@ -175,28 +183,7 @@ public void ConfigureServices(IServiceCollection services)
175183 {
176184 List < string > scopesForCurrentApi = this . apiTypes . GetValueOrDefault ( Enum . Parse < ExamplesApiType > ( this . Configuration [ "API" ] ) ) ;
177185
178- foreach ( var api in this . apiTypes )
179- {
180- if ( this . Configuration [ "API" ] != api . Key . ToString ( ) )
181- {
182- foreach ( var scope in api . Value )
183- {
184- if ( scopesForCurrentApi != null && ! scopesForCurrentApi . Contains ( scope ) )
185- {
186- var scopeWithSeperator = scope + "%20" ;
187-
188- if ( redirectContext . RedirectUri . Contains ( scopeWithSeperator ) )
189- {
190- redirectContext . RedirectUri = redirectContext . RedirectUri . Replace ( scopeWithSeperator , string . Empty ) ;
191- }
192- else
193- {
194- redirectContext . RedirectUri = redirectContext . RedirectUri . Replace ( scope , string . Empty ) ;
195- }
196- }
197- }
198- }
199- }
186+ redirectContext . RedirectUri = this . UpdateRedirectUriScopes ( redirectContext . RedirectUri , scopesForCurrentApi ) ;
200187
201188 redirectContext . HttpContext . Response . Redirect ( redirectContext . RedirectUri ) ;
202189 return Task . FromResult ( 0 ) ;
@@ -327,5 +314,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
327314
328315 return keyValue ;
329316 }
317+
318+ private string UpdateRedirectUriScopes ( string uri , List < string > wantedScopes )
319+ {
320+ const string pattern = @"(?:&|\?)scope=([^&]+)" ;
321+
322+ var encodedScopes = string . Join ( " " , wantedScopes ) ;
323+ return Regex . Replace ( uri , pattern , $ "&scope={ HttpUtility . UrlPathEncode ( encodedScopes ) } ") ;
324+ }
330325 }
331326}
0 commit comments