module nl.netsoj.chris.blog.interfaces.indieauth; import nl.netsoj.chris.blog.interfaces.http; import nl.netsoj.chris.blog.interfaces.micropub; import nl.netsoj.chris.blog.microformats.parser; import mfd = nl.netsoj.chris.blog.microformats.definitions; import vibe.d; struct App { string logo;; string name; string url; string clientId; string redirectUri; bool richInfo = false; } /** * An implementation of https://www.w3.org/TR/indieauth/ */ @translationContext!TranslateContext class IndieAuth { public: this() { m_microPub = new MicroPub(); } @queryParam("response_type", "response_type") @queryParam("me", "me") @queryParam("client_id", "client_id") @queryParam("redirect_uri", "redirect_uri") @queryParam("state", "state") void getAuth(HTTPServerRequest req, HTTPServerResponse res, string response_type, string me, string client_id, string redirect_uri, string state) { enforceHTTP(response_type == "code", HTTPStatus.badRequest); URL source = URL(client_id); HTTPClientResponse response = requestHTTP(source); mfd.App[] parsedApps = parsePage!(mfd.App)(response.bodyReader.readAllUTF8, source); App app; app.name = client_id; app.richInfo = false; if (parsedApps.length > 0) { auto parsedApp = parsedApps[0]; app.logo = parsedApp.logo; app.name = parsedApp.name; app.richInfo = true; } app.clientId = client_id; URL redirectUrl = URL(redirect_uri); redirectUrl.queryString = redirectUrl.queryString ~ "%scode=%s&state=%s".format( (redirectUrl.queryString.length == 0 ? "" : "&"), "123456", state); app.redirectUri = redirectUrl.toString(); render!("pages/indieauth.dt", app); } @safe @path("/") void getIndex(HTTPServerResponse res) { res.writeBody("IndieAuth root", "text/plain"); } @safe void postToken(HTTPServerRequest req, HTTPServerResponse res, string grant_type, string code, string client_id, string redirect_uri, string me) { struct OkResponse { string access_token; string token_type = "Bearer"; string me; string scope_; } OkResponse response; response.me = me; response.scope_ = "foo"; response.access_token = "baz"; res.writeJsonBody(response, HTTPStatus.ok); } @safe @property MicroPub micropub() { return m_microPub; }; private: MicroPub m_microPub; }