Last active
April 11, 2017 09:13
-
-
Save ueki-kazuki/9b178b2f73e85432884cc8da24c53ed3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function (user, context, callback) { | |
if (context.clientID === 'AUTH0_CLIENT_ID') { | |
// set AWS settings | |
context.addonConfiguration = context.addonConfiguration || {}; | |
context.addonConfiguration.aws = context.addonConfiguration.aws || {}; | |
context.addonConfiguration.aws.principal = 'arn:aws:iam::123456789012:saml-provider/auth0-test'; | |
context.addonConfiguration.aws.role = 'arn:aws:iam::123456789012:role/cm-js-sample-role@auth0'; | |
} | |
callback(null, user, context); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.config = { | |
client_id: 'AUTH0_CLIENT_ID', | |
domain: 'YOUR_DOMAIN.auth0.com', | |
bucket: 'YOUR_UPLOAD_BUCKET', | |
region: 'ap-northeast-1' | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>AWS SDK for JavaScript - Sample Application</title> | |
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.12.min.js "></script> | |
<script src="https://cdn.auth0.com/js/lock/10.8/lock.min.js"></script> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | |
<script src="./config.js"></script> | |
</head> | |
<body> | |
<input type="file" id="file-chooser" /> | |
<button id="upload-button" style="display:none">Upload to S3</button> | |
<div id="results"></div> | |
<div id="fb-root"></div> | |
<script type="text/javascript"> | |
var lock = new Auth0Lock(window.config.client_id, window.config.domain); | |
var bucketName = window.config.bucket; | |
AWS.config.region = window.config.region; | |
var auth0UserId; | |
var bucket = new AWS.S3({ | |
params: { | |
Bucket: bucketName | |
} | |
}); | |
var fileChooser = document.getElementById('file-chooser'); | |
var button = document.getElementById('upload-button'); | |
var results = document.getElementById('results'); | |
button.addEventListener('click', function () { | |
var file = fileChooser.files[0]; | |
if (file) { | |
results.innerHTML = ''; | |
//Object key will be auth0-USERID#/FILE_NAME | |
var objKey = 'auth0-' + auth0UserId + '/' + file.name; | |
var params = { | |
Key: objKey, | |
ContentType: file.type, | |
Body: file, | |
ACL: 'public-read' | |
}; | |
bucket.putObject(params, function (err, data) { | |
if (err) { | |
results.innerHTML = 'ERROR: ' + err; | |
} else { | |
listObjs(); | |
} | |
}); | |
} else { | |
results.innerHTML = 'Nothing to upload.'; | |
} | |
}, false); | |
function listObjs() { | |
var prefix = 'auth0-' + auth0UserId; | |
bucket.listObjects({ | |
Prefix: prefix | |
}, function (err, data) { | |
if (err) { | |
results.innerHTML = 'ERROR: ' + err; | |
} else { | |
var objKeys = ""; | |
data.Contents.forEach(function (obj) { | |
objKeys += obj.Key + "<br>"; | |
}); | |
results.innerHTML = objKeys; | |
} | |
}); | |
}; | |
lock.on("authenticated", function(authResult) { | |
console.log("authResult: " + JSON.stringify(authResult)); | |
lock.getUserInfo(authResult.accessToken, function(error, profile) { | |
if (error) { | |
console.log('Error loading the Profile', error) | |
return; | |
} | |
console.log("Profile:" + JSON.stringify(profile)); | |
auth0UserId = profile.nickname; | |
var auth0 = new Auth0({ | |
clientID: window.config.client_id, | |
domain: window.config.domain, | |
callbackURL: 'dummy' | |
}); | |
var aws_creds; | |
var options = { | |
id_token: authResult.idToken, | |
api: 'aws' | |
}; | |
auth0.getDelegationToken(options, function(err, result) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
aws_creds = result.Credentials; // AWS temp credentials | |
console.log("aws_creds: " + JSON.stringify(aws_creds)); | |
bucket.config.credentials = new AWS.Credentials({ | |
accessKeyId: aws_creds.AccessKeyId, | |
secretAccessKey: aws_creds.SecretAccessKey, | |
sessionToken: aws_creds.SessionToken}); | |
}); | |
button.style.display = 'block'; | |
}); | |
}); | |
(function () { | |
if (location.hash == '') { | |
lock.show(); | |
} | |
}()); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment