S3 query string authentication and AWS Security Token Service

Getting this right took some tweaking, so:

// http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth
// http://docs.amazonwebservices.com/STS/latest/APIReference/Welcome.html

var access_key = '…', secret_key = '…', session_token = '…';

var expires = Math.floor(((new Date()).getTime()/1000) + 3600);
var string_to_sign = [
    'GET\n\n\n',
    expires, '\n',
    'x-amz-security-token:', session_token, '\n',
    '/', bucket, '/', key
].join('');

// https://github.com/lmorchard/S3Ajax/blob/master/js/sha1.js
var signature = b64_hmac_sha1(secret_key, string_to_sign) + '=';

var url = key
    + '?AWSAccessKeyId=' + encodeURIComponent(access_key)
    + '&Signature=' + encodeURIComponent(signature)
    + '&Expires=' + expires
    + '&x-amz-security-token=' + encodeURIComponent(session_token);
    

Tagged with:

Categorised as:


(Desktop) Safari & (W3C) Geolocation API

I don't know why this thing fails in desktop Safari. I'm using Modernizr (which is bundled with the highly recommended HTML5 Boilerplate) which reports that the Geolocation API is supported but I'm unable to even get any dialog asking the user for permission to geolocate (which is, if I read the draft correctly, REQUIRED to be implemented). Also, it seems that others (quite a few people actually) have had this same problem but I have been unable to find a solution yet. Below is a snippet from the code; in desktop Safari the PositionErrorCallback (the latter cb function) gets called after timeout but no luck with PositionCallback, no matter how long the timeout value is. Other tested browsers work as expected. Referenced in other places:

(Note to self: check if Google Gears, which is still installed, is causing this?)

[javascript] var position = null; $(document).ready(function() { if(!Modernizr.geolocation) { return; } navigator.geolocation.watchPosition( function(pos) { position = {}; position.lat = pos.coords.latitude; position.lng = pos.coords.longitude; position.allowed = true; init(); }, function(error) { position = {}; position.allowed = false; }, { enableHighAccuracy: false, timeout: 10000, maximumAge: 86400000 } ); checkposition(); }); function checkposition() { log("checkposition()"); if(!position) { setTimeout(function() { checkposition(); }, 1000); return; } else { if(position.allowed) { log("checkposition(): got position: " + position.lat + "," + position.lng); fetchephemeris(); } else { log("checkposition(): could not get a position, giving up"); $("#geolocate").hide(); } } } [/javascript]

Tagged with:

Categorised as: