1 module konnexengine.integrations.google.auth;
2 
3 // import std.conv: to;
4 // import vibe.core.log: logInfo, logWarn;
5 // import vibe.data.json: Json, parseJsonString;
6 // import vibe.stream.operations: readAllUTF8;
7 
8 
9 // string authTokenTarget = "https://oauth2.googleapis.com/token";
10 
11 // struct AuthScopes
12 // {
13 // 	string sheetsReadWrite = "https://www.googleapis.com/auth/spreadsheets";
14 // 	string sheetsReadOnly = "https://www.googleapis.com/auth/spreadsheets.readonly";
15 // }
16 
17 // struct JWTClaim 
18 // {
19 // 	string serviceAccount;
20 // 	string authScope;
21 // 	string target;
22 // }
23 
24 
25 // string createJWTRequestObject(T)(T t)
26 // {
27 // 	Json header = Json(["alg": Json("RS256"), "type": Json("JWT")]);
28 	
29 // 	import std.datetime.systime: stdTimeToUnixTime, Clock;
30 // 	auto issuedAt = stdTimeToUnixTime(Clock.currStdTime());
31 // 	auto expiry = issuedAt + 3599;
32 
33 // 	logInfo(issuedAt.to!string);
34 // 	logInfo(expiry.to!string);
35 	
36 // 	Json claimSet = Json([
37 // 		"iss": Json(t.serviceAccount),
38 // 		"scope": Json(t.authScope),
39 // 		"aud": Json(t.target),
40 // 		"exp": Json(expiry),
41 // 		"iat": Json(issuedAt)
42 // 	]);
43 // 	import std.base64: Base64URLNoPadding;
44 // 	import std.string: representation;
45 // 	string headerEnc = Base64URLNoPadding.encode(header.to!string.representation);
46 // 	string claimEnc = Base64URLNoPadding.encode(claimSet.to!string.representation);
47 // 	return headerEnc ~ "." ~ claimEnc;
48 // }
49 // /// 
50 // unittest
51 // {
52 // 	import fastjwt.jwt;
53 // 	import stringbuffer;
54 
55 // 	string secret = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8rmOWgWUcTyQH\nZcJ1WKxjAqG4gkarkN0nqIuxkOPkLkWXWqjLdyKZ5xnAzZ9iq4pg3ZadzfeaMKgp\nAWI9RTMYqryMMUmXYu5KJpEH4860TqUuj5J2iZOgqC5GJKH2eau1ph3PSZ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W5Zr//p6caG+rZOcCJSKwUK2u1OyDo/0NfZDL79VxjDExAoGAa++H\nIW3iBH/kFM18pF3tI7J2ec80e1SLic3TQUxcEF5iZCqXcDtprbiPI7ZnUOxtn2JV\nB3oOIEbEtCliYQt1RUtxM0tbxd6apdkBYl/M/zK4peLKSbhoEBGHSla6i71nQDhm\nAXF6hdZ5H5Y1iuFEuZZ01a8R0oOeiDxQsASs2oUCgYEAofd5cqb/wijzxiizxCin\nMjCs6Gg7iYHOzKt6S46ME4hcII0zEnWGOp82OrmZ/9OZrB9QkF1eV+9L45GxM5jA\niwrJ5wjvp3gI84/vW8SPPictkXVnCgRRwVlvE+gC7ysMeX0SSt+/YawuNxaiazb0\nBAhROk01HsBKNj+tbvhBVR0=";
56 // 	JWTAlgorithm algo = JWTAlgorithm.HS256;
57 // 	StringBuffer buf;
58 // 	auto claim = JWTClaim("tbb-ml@the-beat-box-connector.iam.gserviceaccount.com", "https://www.googleapis.com/auth/spreadsheets.readonly", authTokenTarget);
59 // 	auto set = createJWTRequestObject!JWTClaim(claim);
60 
61 // 	encodeJWTToken(buf, algo, secret, set);
62 // 	// res.writeBody();
63 
64 	
65 // 	logInfo(buf.getData());
66 // }
67 
68 // string requestAPIAccessToken(T)(T t)
69 // {
70 // 	import vibe.http.common: HTTPMethod;
71 // 	import vibe.http.client: requestHTTP;
72 // 	import vibe.textfilter.urlencode: formEncode;
73 	
74 
75 // 	string url = authTokenTarget;
76 // 	string result = "";
77 
78 // 	requestHTTP(url, (scope req) {
79 // 		req.method = HTTPMethod.POST;
80 // 		req.headers["Content-Type"] = "application/x-www-form-urlencoded"; 
81 // 		req.writeFormBody([
82 // 			"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
83 // 			"assertion": t
84 // 		]);
85 // 	}, (scope res) {
86 // 		result = res.bodyReader.readAllUTF8();
87 // 		logWarn("\nGoogle OAuth2 Token Service --- Response: %s\n", parseJsonString(result).toPrettyString());
88 // 		// if (res.statusCode == 200)
89 // 		// {
90 // 		// 	auto r = result;
91 // 		// }
92 		
93 // 	});
94 // 	return result;
95 // }
96 // ///
97 // unittest
98 // {
99 // 	// import fastjwt.jwt;
100 // 	// import stringbuffer;
101 
102 // 	string secret = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8rmOWgWUcTyQH\nZcJ1WKxjAqG4gkarkN0nqIuxkOPkLkWXWqjLdyKZ5xnAzZ9iq4pg3ZadzfeaMKgp\nAWI9RTMYqryMMUmXYu5KJpEH4860TqUuj5J2iZOgqC5GJKH2eau1ph3PSZFqBxzp\nABYPpaweLd2zr7b5ywwnuFLfvbNoiazYzJnRxpJgFzZgRBuWZz7pHaJBIvFNuCMt\n/PyVh8NGCYHpEROaSow5R86Vuo0dfjDLwpW4/WE5arc3GzRjIsXacsJUq0L+Bcad\nDWeWKPWJ4zWhn6rp/0t33yOv+p7GFJpqbWlHUeUZNQnf6cr8oU0JCX2cAP9ZicN9\n+MWtU+vHAgMBAAECggEAA1sAl2UetLWtmNoU7rxQeIRL9vmOIy/01uc28MX4X475\nGtt4ZAHu4hjLivO9MWaGk/ls28qVbC8QVoZZRmisI3Sp3L5lpiUSwsvTyXWiL++0\nHVNdUfFZaEKFbCbroHQCez0Ei7KtvNp6h6RCQC38lR7QsgjecqvLQAuVobgAcEv2\npOcasWOi8Xk3MMLjh8HJLFJ/3F4jigG0w9iQj/LSFwxTi/r5GFekQAVTSAqymbG3\n1rSGlK9T3J9O8uAmqf5xLHfiScbE9Zbz6xddxA7qnwEFri+OKkeaOJ0dQbpdx++a\no91CN5AaFi/niuMrPR7A2x4LD4FvMZQKrtvrrhv3ZQKBgQDzVDnFhZ3JDSwXoaBk\ne8MKgALnumFhM9Hbjp0+ahXKL/bRFeuznSw2Rit58zR1CvKlcHg5gu4uQuqK+VCX\nf7hVQzy4KL8s9QXkICf6B2FDvDcH/e6SopgqTVNbWbphuy3UsWYsk2y58qOQ1XCW\nUaxw/Mw1BkKcu8VXmWCii+zPgwKBgQDGganfD8skrGPj/m4OmhPpRGKfifDq5Mz1\n5ugqH7hAgC50kuUAX1vtQGkIAb7zonRdP1CqzIncwxDLqmK15XqoLm0ddyOa9Ard\niT5quAirx9IG74dI238uTDA3u7ONQfLWF/BEGaHQQzv6rdhlQaMLvWhnw6a6sUOm\nc3Pi/JRbbQKBgHhTdh4O7O+2687jxgTqjpBqrQUaTX1burLv2yI6I3xQK6VJSb0Q\nRASyvl0XvyEBwOu+qjUGYfHOdV0z3H6OUgEXtrb6r07z+23L2PjzHU35jN0O3SL1\n9Hk2s7nArUIW5Zr//p6caG+rZOcCJSKwUK2u1OyDo/0NfZDL79VxjDExAoGAa++H\nIW3iBH/kFM18pF3tI7J2ec80e1SLic3TQUxcEF5iZCqXcDtprbiPI7ZnUOxtn2JV\nB3oOIEbEtCliYQt1RUtxM0tbxd6apdkBYl/M/zK4peLKSbhoEBGHSla6i71nQDhm\nAXF6hdZ5H5Y1iuFEuZZ01a8R0oOeiDxQsASs2oUCgYEAofd5cqb/wijzxiizxCin\nMjCs6Gg7iYHOzKt6S46ME4hcII0zEnWGOp82OrmZ/9OZrB9QkF1eV+9L45GxM5jA\niwrJ5wjvp3gI84/vW8SPPictkXVnCgRRwVlvE+gC7ysMeX0SSt+/YawuNxaiazb0\nBAhROk01HsBKNj+tbvhBVR0=\n-----END PRIVATE KEY-----\n";
103 // 	// JWTAlgorithm algo = JWTAlgorithm.HS256;
104 // 	// StringBuffer buf;
105 // 	auto claim = JWTClaim("tbb-ml@the-beat-box-connector.iam.gserviceaccount.com", "https://www.googleapis.com/auth/spreadsheets.readonly", authTokenTarget);
106 // 	auto input = createJWTRequestObject!JWTClaim(claim);
107 
108 // 	// encodeJWTToken(buf, algo, secret, set);
109 // 	// res.writeBody();
110 
111 // 	string assertion = buf.getData();
112 // 	logInfo(buf.getData());
113 
114 // 	string token = requestAPIAccessToken!string(assertion);
115 
116 // 	logInfo(token);
117 // }