From 6fe226e0ccc36e82da1ddd9f7ea1ccf72bc7cbfa Mon Sep 17 00:00:00 2001 From: Parminder Singh Date: Fri, 2 Mar 2018 20:44:33 +0530 Subject: [PATCH] Implement initial setup for the API handler --- .../clientapi/routing/auth_fallback.go | 106 ++++++++++++++++++ .../dendrite/clientapi/routing/routing.go | 7 ++ .../matrix-org/dendrite/common/httpapi.go | 13 +++ 3 files changed, 126 insertions(+) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go b/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go new file mode 100644 index 000000000..0e93ef801 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/auth_fallback.go @@ -0,0 +1,106 @@ +// Copyright 2017 Vector Creations Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package routing + +import ( + "html/template" + "net/http" +) + +// RecaptchaTemplate is template for recaptcha auth +const RecaptchaTemplate = ` + + +Authentication + + + + + + + +
+
+

+ Hello! We need to prevent computer programs and other automated + things from creating accounts on this server. +

+

+ Please verify that you're not a robot. +

+ +
+
+ +
+ +
+ + +` + +// SuccessTemplate is template for success page after auth flow ends +const SuccessTemplate = ` + + +Success! + + + + + +
+

Thank you

+

You may now close this window and return to the application

+
+ + +` + +// AuthFallback implements GET on /auth/{authType}/fallback/web?session={sessionID} +func AuthFallback(w http.ResponseWriter, req *http.Request, authType string, sessionID string) { + if req.Method == "GET" { + t := template.Must(template.New("response").Parse(RECAPTCHA_TEMPLATE)) + + data := map[string]string{ + "MyUrl": "TODO", + "Session": "TODO", + "SiteKey": "TODO", + } + + if err := t.Execute(w, data); err != nil { + panic(err) + } + // TODO: Handle rest of flow + } + // TODO: Handle invalid request +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index a7999bf49..9ae40aa80 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -174,6 +174,13 @@ func Setup( }), ).Methods("GET", "POST", "OPTIONS") + r0mux.Handle("/auth/{authType}/fallback/web?session={sessionID}", + common.MakeHtmlAPI("authfallback", func(w http.ResponseWriter, req *http.Request) util.JSONResponse { + vars := mux.Vars(req) + return AuthFallback(w, req, vars["authType"], vars["sessionID"]) + }), + ).Methods("GET", "OPTIONS") + r0mux.Handle("/pushrules/", common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse { // TODO: Implement push rules API diff --git a/src/github.com/matrix-org/dendrite/common/httpapi.go b/src/github.com/matrix-org/dendrite/common/httpapi.go index b2ef89597..076845bbe 100644 --- a/src/github.com/matrix-org/dendrite/common/httpapi.go +++ b/src/github.com/matrix-org/dendrite/common/httpapi.go @@ -39,6 +39,19 @@ func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan)) } +// MakeHTMLAPI adds Span metrics to the HTML Handler function +// This is used to serve HTML template +func MakeHtmlAPI(metricsName string, f func(w http.ResponseWriter, *http.Request)) http.Handler { + withSpan := func(w http.ResponseWriter, req *http.Request) { + span := opentracing.StartSpan(metricsName) + defer span.Finish() + req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span)) + f(w, req) + } + + return prometheus.InstrumentHandler(metricsName, http.HandlerFunc(withSpan)) +} + // MakeInternalAPI turns a util.JSONRequestHandler function into an http.Handler. // This is used for APIs that are internal to dendrite. // If we are passed a tracing context in the request headers then we use that