# Mock OAuth2 provider for the demo: a token endpoint and an introspection# endpoint (the one Dovecot asks). Not part of the talk; it is a stand-in, not a real provider.use io;
use httpd;
use strings;
use encoding;
defconstTOKENasstringinit"ya29.demo-access-token";
def srv ashttpd.Server inithttpd.listen("0.0.0.0:8080");
io.printf("mock oauth on :8080\n");
while (true) { # lint-disable: L105def req ashttpd.Request inithttpd.accept($srv);
def body asstringinitencoding.decode("utf-8", httpd.body($req));
io.printf("%s %s %s\n", httpd.method($req), httpd.path($req), $body);
if (httpd.path($req) == "/token") {
httpd.setHeader($req, "Content-Type", "application/json");
if (strings.contains($body, "refresh_token=rt-demo")) {
httpd.respond($req, 200, '{"access_token":"' + TOKEN +
'","token_type":"Bearer","expires_in":3599}');
} else {
httpd.respond($req, 400, '{"error":"invalid_grant"}');
}
} elseif (httpd.path($req) == "/introspect") {
httpd.setHeader($req, "Content-Type", "application/json");
if (strings.contains($body, TOKEN)) {
httpd.respond($req, 200, '{"active":true,"username":"ada@example.org"}');
} else {
httpd.respond($req, 200, '{"active":false}');
}
} else {
httpd.respond($req, 404, "not found");
}
}