Skip to content

Commit 3b148a5

Browse files
committed
fix bug in authorization #123
1 parent a168d24 commit 3b148a5

File tree

5 files changed

+49
-79
lines changed

5 files changed

+49
-79
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ static async Task Main(string[] args)
3939
{
4040
AccessKey = config.GetSection("MWSAmazonAPI:AccessKey").Value,
4141
SecretKey = config.GetSection("MWSAmazonAPI:SecretKey").Value,
42+
RoleArn = config.GetSection("MWSAmazonAPI:RoleArn").Value,
4243
ClientId = config.GetSection("MWSAmazonAPI:ClientId").Value,
4344
ClientSecret = config.GetSection("MWSAmazonAPI:ClientSecret").Value,
44-
MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("FikaAmazonAPI:MarketPlaceID").Value),
45+
MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("MWSAmazonAPI:MarketPlaceID").Value),
4546
IsActiveLimitRate = true
4647
});
4748
var code = codeAmazonConnection.Authorization.GetAuthorizationCode(new Parameter.Authorization.ParameterAuthorizationCode()

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Token/CacheTokenData.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class CacheTokenData
77
protected TokenResponse NormalAccessToken { get; set; }
88
protected TokenResponse PIIAccessToken { get; set; }
99
protected TokenResponse GrantlessAccessToken { get; set; }
10-
protected TokenResponse MigrationAccessToken { get; set; }
1110
protected AWSAuthenticationTokenData AWSAuthenticationTokenData { get; set; }
1211

1312
public AWSAuthenticationTokenData GetAWSAuthenticationTokenData()
@@ -35,10 +34,6 @@ public TokenResponse GetToken(TokenDataType tokenDataType)
3534
{
3635
token = GrantlessAccessToken;
3736
}
38-
else if (tokenDataType == TokenDataType.MigrationOnly)
39-
{
40-
token = MigrationAccessToken;
41-
}
4237
if (token == null)
4338
return null;
4439
else
@@ -66,18 +61,13 @@ public void SetToken(TokenDataType tokenDataType, TokenResponse token)
6661
{
6762
GrantlessAccessToken = token;
6863
}
69-
else if (tokenDataType == TokenDataType.MigrationOnly)
70-
{
71-
MigrationAccessToken = token;
72-
}
7364
}
7465

7566
public enum TokenDataType
7667
{
7768
Normal,
7869
PII,
79-
Grantless,
80-
MigrationOnly
70+
Grantless
8171
}
8272

8373

Source/FikaAmazonAPI/Services/AuthorizationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public string GetAuthorizationCode(ParameterAuthorizationCode parameterGetOrderM
1616
public async Task<string> GetAuthorizationCodeAsync(ParameterAuthorizationCode parameter)
1717
{
1818
var param = parameter.getParameters();
19-
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.GET, param, tokenDataType: TokenDataType.MigrationOnly);
19+
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.GET, param, tokenDataType: TokenDataType.Grantless);
2020

2121
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>(Utils.RateLimitType.Authorization_GetAuthorizationCode);
2222
if (response != null && response.Payload != null)

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void CreateAuthorizedPagedRequest(AmazonFilter filter, string url, Res
102102
{
103103
RestHeader();
104104
AddAccessToken();
105-
Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.BaseUrl.Host, AmazonCredential, rateLimitType == RateLimitType.Authorization_GetAuthorizationCode);
105+
Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.BaseUrl.Host, AmazonCredential);
106106
var response = await RequestClient.ExecuteAsync<T>(Request);
107107
SleepForRateLimit(response.Headers, rateLimitType);
108108
ParseResponse(response);

Source/FikaAmazonAPI/Services/TokenGeneration.cs

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,21 @@ public static class TokenGeneration
2121

2222
public static async Task<TokenResponse> RefreshAccessTokenAsync(AmazonCredential credentials, TokenDataType tokenDataType = TokenDataType.Normal)
2323
{
24-
if (tokenDataType == TokenDataType.MigrationOnly)
24+
var lwaCredentials = new LWAAuthorizationCredentials()
2525
{
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;
4739
}
4840

4941
public static async Task<TokenResponse> GetAccessTokenForSPAPIMigration(string ClientId, string ClientSecret)
@@ -70,60 +62,47 @@ public static async Task<TokenResponse> GetAccessTokenForSPAPIMigration(string C
7062
return JsonConvert.DeserializeObject<TokenResponse>(data);
7163
}
7264

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)
7466
{
75-
if (isMigration)
67+
var dataToken = amazonCredential.GetAWSAuthenticationTokenData();
68+
if (dataToken == null)
7669
{
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
7784
var awsAuthenticationCredentials = new AWSAuthenticationCredentials
7885
{
79-
AccessKeyId = amazonCredential.AccessKey,
80-
SecretKey = amazonCredential.SecretKey,
86+
AccessKeyId = response1.Credentials.AccessKeyId,
87+
SecretKey = response1.Credentials.SecretAccessKey,
8188
Region = amazonCredential.MarketPlace.Region.RegionName
8289
};
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()
9092
{
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+
}
11199

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-
}
120100

101+
restRequest.AddOrUpdateHeader(RequestService.SecurityTokenHeaderName, dataToken.SessionToken);
121102

122-
restRequest.AddOrUpdateHeader(RequestService.SecurityTokenHeaderName, dataToken.SessionToken);
103+
return new AWSSigV4Signer(dataToken.AWSAuthenticationCredential)
104+
.Sign(restRequest, host);
123105

124-
return new AWSSigV4Signer(dataToken.AWSAuthenticationCredential)
125-
.Sign(restRequest, host);
126-
}
127106
}
128107
}
129108
}

0 commit comments

Comments
 (0)