| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package matrix |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "net/http" |
| 23 | "net/url" |
| 24 | "time" |
| 25 | |
| 26 | "maunium.net/go/mautrix" |
| 27 | |
| 28 | "maunium.net/go/gomuks/debug" |
| 29 | "maunium.net/go/gomuks/lib/open" |
| 30 | ) |
| 31 | |
| 32 | const uiaFallbackPage = `<!DOCTYPE html> |
| 33 | <html lang="en"> |
| 34 | <head> |
| 35 | <title>gomuks user-interactive auth</title> |
| 36 | <meta charset="utf-8"/> |
| 37 | <style> |
| 38 | body { |
| 39 | text-align: center; |
| 40 | } |
| 41 | </style> |
| 42 | </head> |
| 43 | <body> |
| 44 | <h2>Please complete the login in the popup window</h2> |
| 45 | <p>Keep this page open while logging in, it will close automatically after the login finishes.</p> |
| 46 | <button onclick="openPopup()">Open popup</button> |
| 47 | <button onclick="finish(false)">Cancel</button> |
| 48 | <script> |
| 49 | const url = location.hash.substr(1) |
| 50 | let popupWindow |
| 51 | |
| 52 | function finish(success) { |
| 53 | if (popupWindow) { |
| 54 | popupWindow.close() |
| 55 | } |
| 56 | fetch("", {method: success ? "POST" : "DELETE"}).then(() => window.close()) |
| 57 | } |
| 58 | |
| 59 | function openPopup() { |
| 60 | popupWindow = window.open(url) |
| 61 | } |
| 62 | |
| 63 | window.addEventListener("message", evt => evt.data === "authDone" && finish(true)) |
| 64 | </script> |
| 65 | </body> |
| 66 | </html> |
| 67 | ` |
| 68 | |
| 69 | func (c *Container) UIAFallback(loginType mautrix.AuthType, sessionID string) error { |
| 70 | errChan := make(chan error, 1) |
| 71 | server := &http.Server{Addr: ":29325"} |
| 72 | server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 73 | if r.Method == "GET" { |
| 74 | w.Header().Add("Content-Type", "text/html") |
| 75 | w.WriteHeader(http.StatusOK) |
| 76 | _, _ = w.Write([]byte(uiaFallbackPage)) |
| 77 | } else if r.Method == "POST" || r.Method == "DELETE" { |
| 78 | w.Header().Add("Content-Type", "text/html") |
| 79 | w.WriteHeader(http.StatusOK) |
| 80 | |
| 81 | go func() { |
| 82 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 83 | defer cancel() |
| 84 | err := server.Shutdown(ctx) |
| 85 | if err != nil { |
| 86 | debug.Printf("Failed to shut down SSO server: %v\n", err) |
| 87 | } |
| 88 | if r.Method == "DELETE" { |
| 89 | errChan <- errors.New("login cancelled") |
| 90 | } else { |
| 91 | errChan <- nil |
| 92 | } |
| 93 | }() |
| 94 | } else { |
| 95 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 96 | } |
| 97 | }) |
| 98 | go server.ListenAndServe() |
| 99 | defer server.Close() |
| 100 | authURL := c.client.BuildURLWithQuery(mautrix.ClientURLPath{"v3", "auth", loginType, "fallback", "web"}, map[string]string{ |
| 101 | "session": sessionID, |
| 102 | }) |
| 103 | link := url.URL{ |
| 104 | Scheme: "http", |
| 105 | Host: "localhost:29325", |
| 106 | Path: "/", |
| 107 | Fragment: authURL, |
| 108 | } |
| 109 | err := open.Open(link.String()) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | err = <-errChan |
| 114 | return err |
| 115 | } |