@@ -21,29 +21,21 @@ public static class TokenGeneration
21
21
22
22
public static async Task < TokenResponse > RefreshAccessTokenAsync ( AmazonCredential credentials , TokenDataType tokenDataType = TokenDataType . Normal )
23
23
{
24
- if ( tokenDataType == TokenDataType . MigrationOnly )
24
+ var lwaCredentials = new LWAAuthorizationCredentials ( )
25
25
{
26
- var accessToken = await GetAccessTokenForSPAPIMigration ( credentials . ClientId , credentials . ClientSecret ) ;
27
- return accessToken ;
28
- }
29
- else
30
- {
31
- var lwaCredentials = new LWAAuthorizationCredentials ( )
32
- {
33
- ClientId = credentials . ClientId ,
34
- ClientSecret = credentials . ClientSecret ,
35
- Endpoint = new Uri ( Constants . AmazonToeknEndPoint ) ,
36
- RefreshToken = credentials . RefreshToken ,
37
- Scopes = null
38
- } ;
39
- if ( tokenDataType == TokenDataType . Grantless )
40
- lwaCredentials . Scopes = new List < string > ( ) { ScopeConstants . ScopeMigrationAPI , ScopeConstants . ScopeNotificationsAPI } ;
41
-
42
- var Client = new LWAClient ( lwaCredentials ) ;
43
- var accessToken = await Client . GetAccessTokenAsync ( ) ;
44
-
45
- return accessToken ;
46
- }
26
+ ClientId = credentials . ClientId ,
27
+ ClientSecret = credentials . ClientSecret ,
28
+ Endpoint = new Uri ( Constants . AmazonToeknEndPoint ) ,
29
+ RefreshToken = credentials . RefreshToken ,
30
+ Scopes = null
31
+ } ;
32
+ if ( tokenDataType == TokenDataType . Grantless )
33
+ lwaCredentials . Scopes = new List < string > ( ) { ScopeConstants . ScopeMigrationAPI , ScopeConstants . ScopeNotificationsAPI } ;
34
+
35
+ var Client = new LWAClient ( lwaCredentials ) ;
36
+ var accessToken = await Client . GetAccessTokenAsync ( ) ;
37
+
38
+ return accessToken ;
47
39
}
48
40
49
41
public static async Task < TokenResponse > GetAccessTokenForSPAPIMigration ( string ClientId , string ClientSecret )
@@ -70,60 +62,47 @@ public static async Task<TokenResponse> GetAccessTokenForSPAPIMigration(string C
70
62
return JsonConvert . DeserializeObject < TokenResponse > ( data ) ;
71
63
}
72
64
73
- public static async Task < IRestRequest > SignWithSTSKeysAndSecurityTokenAsync ( IRestRequest restRequest , string host , AmazonCredential amazonCredential , bool isMigration = false )
65
+ public static async Task < IRestRequest > SignWithSTSKeysAndSecurityTokenAsync ( IRestRequest restRequest , string host , AmazonCredential amazonCredential )
74
66
{
75
- if ( isMigration )
67
+ var dataToken = amazonCredential . GetAWSAuthenticationTokenData ( ) ;
68
+ if ( dataToken == null )
76
69
{
70
+ AssumeRoleResponse response1 = null ;
71
+ using ( var STSClient = new AmazonSecurityTokenServiceClient ( amazonCredential . AccessKey , amazonCredential . SecretKey ) )
72
+ {
73
+ var req = new AssumeRoleRequest ( )
74
+ {
75
+ RoleArn = amazonCredential . RoleArn ,
76
+ DurationSeconds = 3600 ,
77
+ RoleSessionName = Guid . NewGuid ( ) . ToString ( )
78
+ } ;
79
+
80
+ response1 = await STSClient . AssumeRoleAsync ( req , new CancellationToken ( ) ) ;
81
+ }
82
+
83
+ //auth step 3
77
84
var awsAuthenticationCredentials = new AWSAuthenticationCredentials
78
85
{
79
- AccessKeyId = amazonCredential . AccessKey ,
80
- SecretKey = amazonCredential . SecretKey ,
86
+ AccessKeyId = response1 . Credentials . AccessKeyId ,
87
+ SecretKey = response1 . Credentials . SecretAccessKey ,
81
88
Region = amazonCredential . MarketPlace . Region . RegionName
82
89
} ;
83
- return new AWSSigV4Signer ( awsAuthenticationCredentials )
84
- . Sign ( restRequest , host ) ;
85
- }
86
- else
87
- {
88
- var dataToken = amazonCredential . GetAWSAuthenticationTokenData ( ) ;
89
- if ( dataToken == null )
90
+
91
+ amazonCredential . SetAWSAuthenticationTokenData ( new AWSAuthenticationTokenData ( )
90
92
{
91
- AssumeRoleResponse response1 = null ;
92
- using ( var STSClient = new AmazonSecurityTokenServiceClient ( amazonCredential . AccessKey , amazonCredential . SecretKey ) )
93
- {
94
- var req = new AssumeRoleRequest ( )
95
- {
96
- RoleArn = amazonCredential . RoleArn ,
97
- DurationSeconds = 3600 ,
98
- RoleSessionName = Guid . NewGuid ( ) . ToString ( )
99
- } ;
100
-
101
- response1 = await STSClient . AssumeRoleAsync ( req , new CancellationToken ( ) ) ;
102
- }
103
-
104
- //auth step 3
105
- var awsAuthenticationCredentials = new AWSAuthenticationCredentials
106
- {
107
- AccessKeyId = response1 . Credentials . AccessKeyId ,
108
- SecretKey = response1 . Credentials . SecretAccessKey ,
109
- Region = amazonCredential . MarketPlace . Region . RegionName
110
- } ;
93
+ AWSAuthenticationCredential = awsAuthenticationCredentials ,
94
+ SessionToken = response1 . Credentials . SessionToken ,
95
+ Expiration = response1 . Credentials . Expiration
96
+ } ) ;
97
+ dataToken = amazonCredential . GetAWSAuthenticationTokenData ( ) ;
98
+ }
111
99
112
- amazonCredential . SetAWSAuthenticationTokenData ( new AWSAuthenticationTokenData ( )
113
- {
114
- AWSAuthenticationCredential = awsAuthenticationCredentials ,
115
- SessionToken = response1 . Credentials . SessionToken ,
116
- Expiration = response1 . Credentials . Expiration
117
- } ) ;
118
- dataToken = amazonCredential . GetAWSAuthenticationTokenData ( ) ;
119
- }
120
100
101
+ restRequest . AddOrUpdateHeader ( RequestService . SecurityTokenHeaderName , dataToken . SessionToken ) ;
121
102
122
- restRequest . AddOrUpdateHeader ( RequestService . SecurityTokenHeaderName , dataToken . SessionToken ) ;
103
+ return new AWSSigV4Signer ( dataToken . AWSAuthenticationCredential )
104
+ . Sign ( restRequest , host ) ;
123
105
124
- return new AWSSigV4Signer ( dataToken . AWSAuthenticationCredential )
125
- . Sign ( restRequest , host ) ;
126
- }
127
106
}
128
107
}
129
108
}
0 commit comments