| 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 matrixscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/ |
| 18 | |
| 19 | import ( |
| 20 | "contextscip-go gomod github.com/golang/go/src go1.21 context/" |
| 21 | "errorsscip-go gomod github.com/golang/go/src go1.21 errors/" |
| 22 | "net/httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/" |
| 23 | "net/urlscip-go gomod github.com/golang/go/src go1.21 `net/url`/" |
| 24 | "timescip-go gomod github.com/golang/go/src go1.21 time/" |
| 25 | |
| 26 | "maunium.net/go/mautrixscip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/" |
| 27 | |
| 28 | "maunium.net/go/gomuks/debugscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/debug`/" |
| 29 | "maunium.net/go/gomuks/lib/openscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/open`/" |
| 30 | ) |
| 31 | |
| 32 | const uiaFallbackPagescip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/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 *Containerscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/Container#) UIAFallbackscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/Container#UIAFallback().(loginType mautrixscip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/.AuthTypescip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/AuthType#, sessionID string) error { |
| 70 | errChan := make(chan error, 1) |
| 71 | server := &httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.Serverscip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#{Addrscip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#Addr.: ":29325"} |
| 72 | server.Handlerscip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#Handler. = httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.21 `net/http`/HandlerFunc#(func(w httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.21 `net/http`/Request#) { |
| 73 | if r.Methodscip-go gomod github.com/golang/go/src go1.21 `net/http`/Request#Method. == "GET" { |
| 74 | w.Headerscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#Header().().Addscip-go gomod github.com/golang/go/src go1.21 `net/http`/Header#Add().("Content-Type", "text/html") |
| 75 | w.WriteHeaderscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.21 `net/http`/StatusOK.) |
| 76 | _, _ = w.Writescip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#Write().([]byte(uiaFallbackPagescip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/uiaFallbackPage.)) |
| 77 | } else if r.Methodscip-go gomod github.com/golang/go/src go1.21 `net/http`/Request#Method. == "POST" || r.Methodscip-go gomod github.com/golang/go/src go1.21 `net/http`/Request#Method. == "DELETE" { |
| 78 | w.Headerscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#Header().().Addscip-go gomod github.com/golang/go/src go1.21 `net/http`/Header#Add().("Content-Type", "text/html") |
| 79 | w.WriteHeaderscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.21 `net/http`/StatusOK.) |
| 80 | |
| 81 | go func() { |
| 82 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.21 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.21 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.21 context/.Backgroundscip-go gomod github.com/golang/go/src go1.21 context/Background().(), 5*timescip-go gomod github.com/golang/go/src go1.21 time/.Secondscip-go gomod github.com/golang/go/src go1.21 time/Second.) |
| 83 | defer cancel() |
| 84 | err := server.Shutdownscip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#Shutdown().(ctx) |
| 85 | if err != nil { |
| 86 | debugscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/debug`/.Printfscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/debug`/Printf().("Failed to shut down SSO server: %v\n", err) |
| 87 | } |
| 88 | if r.Methodscip-go gomod github.com/golang/go/src go1.21 `net/http`/Request#Method. == "DELETE" { |
| 89 | errChan <- errorsscip-go gomod github.com/golang/go/src go1.21 errors/.Newscip-go gomod github.com/golang/go/src go1.21 errors/New().("login cancelled") |
| 90 | } else { |
| 91 | errChan <- nil |
| 92 | } |
| 93 | }() |
| 94 | } else { |
| 95 | w.WriteHeaderscip-go gomod github.com/golang/go/src go1.21 `net/http`/ResponseWriter#WriteHeader().(httpscip-go gomod github.com/golang/go/src go1.21 `net/http`/.StatusMethodNotAllowedscip-go gomod github.com/golang/go/src go1.21 `net/http`/StatusMethodNotAllowed.) |
| 96 | } |
| 97 | }) |
| 98 | go server.ListenAndServescip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#ListenAndServe().() |
| 99 | defer server.Closescip-go gomod github.com/golang/go/src go1.21 `net/http`/Server#Close().() |
| 100 | authURL := c.clientscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/matrix`/Container#client..BuildURLWithQueryscip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/Client#BuildURLWithQuery().(mautrixscip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/.ClientURLPathscip-go gomod maunium.net/go/mautrix fa19263891f5 `maunium.net/go/mautrix`/ClientURLPath#{"v3", "auth", loginType, "fallback", "web"}, map[string]string{ |
| 101 | "session": sessionID, |
| 102 | }) |
| 103 | link := urlscip-go gomod github.com/golang/go/src go1.21 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#{ |
| 104 | Schemescip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#Scheme.: "http", |
| 105 | Hostscip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#Host.: "localhost:29325", |
| 106 | Pathscip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#Path.: "/", |
| 107 | Fragmentscip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#Fragment.: authURL, |
| 108 | } |
| 109 | err := openscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/open`/.Openscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/open`/Open().(link.Stringscip-go gomod github.com/golang/go/src go1.21 `net/url`/URL#String().()) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | err = <-errChan |
| 114 | return err |
| 115 | } |