Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose IdentityId in cognito_credentials to upload to S3 user folder #22

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ print(credentials.sessionToken);

#### For S3 Uploads

S3 Uploads using HTTP Presigned Post.
S3 Upload to user "folder" using HTTP Presigned Post.

```dart
import 'dart:convert';
Expand Down Expand Up @@ -466,6 +466,7 @@ class Policy {

@override
String toString() {
// Safe to remove the "acl" line if your bucket has no ACL permissions
return '''
{ "expiration": "${this.expiration}",
"conditions": [
Expand Down Expand Up @@ -517,8 +518,12 @@ void main() async {
final multipartFile = http.MultipartFile('file', stream, length,
filename: path.basename(file.path));

final String fileName = 'square-cinnamon.jpg';
final String usrIdentityId = _credentials.userIdentityId;
final String bucketKey = 'test/$usrIdentityId/$fileName';

final policy = Policy.fromS3PresignedPost(
'test/square-cinnamon.jpg',
bucketKey,
'my-s3-bucket',
15,
_credentials.accessKeyId,
Expand All @@ -531,7 +536,7 @@ void main() async {

req.files.add(multipartFile);
req.fields['key'] = policy.key;
req.fields['acl'] = 'public-read';
req.fields['acl'] = 'public-read'; // Safe to remove this if your bucket has no ACL permissions
req.fields['X-Amz-Credential'] = policy.credential;
req.fields['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
req.fields['X-Amz-Date'] = policy.datetime;
Expand All @@ -550,6 +555,19 @@ void main() async {
}
```

The role attached to the identity pool (e.g. the ..._poolAuth role) should contain a policy like this:

```json
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": "arn:aws:s3:::BUCKET_NAME/test/${cognito-identity.amazonaws.com:sub}/*"
}
```

#### For S3 GET Object

Signing S3 GET Object using `Authorization` header.
Expand Down Expand Up @@ -866,4 +884,3 @@ CognitoCredentials _credential = new CognitoCredentials('ap-southeast-1_xxxxxxxx
await _credential.getAwsCredentials(accessToken, 'graph.facebook.com')
print(_credential.sessionToken);
```

6 changes: 4 additions & 2 deletions lib/src/cognito_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CognitoCredentials {
String secretAccessKey;
String sessionToken;
int expireTime;
String userIdentityId;

CognitoCredentials(String identityPoolId, CognitoUserPool pool,
{String region, String userPoolId}) {
_pool = pool;
Expand All @@ -30,14 +32,14 @@ class CognitoCredentials {
if (expireTime == null ||
DateTime.now().millisecondsSinceEpoch > expireTime - 60000) {
final identityId = CognitoIdentityId(_identityPoolId, _pool);
final identityIdId = await identityId.getIdentityId(token, authenticator);
furaiev marked this conversation as resolved.
Show resolved Hide resolved
userIdentityId = await identityId.getIdentityId(token, authenticator);

authenticator ??= 'cognito-idp.$_region.amazonaws.com/$_userPoolId';
final Map<String, String> loginParam = {
authenticator: token,
};
final Map<String, dynamic> paramsReq = {
'IdentityId': identityIdId,
'IdentityId': userIdentityId,
'Logins': loginParam,
};

Expand Down