1 module amazonlogin; 2 3 import vibe.data.serialization:optional; 4 5 /// 6 struct TokenInfo 7 { 8 /// this needs to be validated against our Client-Id 9 string aud; 10 /// 11 long exp; 12 /// 13 string iss; 14 /// 15 string user_id; 16 /// 17 string app_id; 18 /// 19 long iat; 20 } 21 22 /// 23 struct UserProfile 24 { 25 /// 26 @optional 27 string name; 28 /// 29 @optional 30 string email; 31 /// 32 string user_id; 33 } 34 35 /// 36 interface AmazonLoginApi 37 { 38 import vibe.web.rest:method,path; 39 import vibe.http.common:HTTPMethod; 40 41 /// 42 @path("auth/o2/tokeninfo") 43 @method(HTTPMethod.GET) 44 TokenInfo tokeninfo(string access_token); 45 46 /// 47 @path("user/profile") 48 @method(HTTPMethod.GET) 49 UserProfile profile(); 50 } 51 52 /// 53 alias AmazonLoginApiFactory = AmazonLoginApi function(string); 54 /// 55 static AmazonLoginApi createAmazonLoginApi(string access_token) 56 { 57 import vibe.web.rest:RestInterfaceClient; 58 import vibe.http.client:HTTPClientRequest; 59 auto res = new RestInterfaceClient!AmazonLoginApi("https://api.amazon.com/"); 60 61 res.requestFilter = (HTTPClientRequest req){ 62 import std.algorithm:endsWith; 63 if(req.requestURL.endsWith("user/profile")) 64 { 65 req.headers["Authorization"] = "bearer "~access_token; 66 } 67 }; 68 69 return res; 70 }