aboutsummaryrefslogtreecommitdiff
path: root/server/web.go
diff options
context:
space:
mode:
authorKevin Lyda <kevin@ie.suberic.net>2018-08-10 17:21:02 +0000
committerNiall Sheridan <nsheridan@gmail.com>2018-08-10 18:21:02 +0100
commit3e006c39b0a4411e91e80de261d0e7b5353d44c0 (patch)
tree57754a0a8b80200eede4d6e5a8c1ae7621a3f393 /server/web.go
parent99a01f63f51b73f103cd1e094f1a8e7f35d9d30b (diff)
Add Microsoft auth provider
Microsoft uses JSON Web Tokens (JWT) as OAuth tokens. These can run to many thousands of characters which are too long for TTYs. Work around this by base64-encoding the token and chunk it into smaller pieces. Closes #70
Diffstat (limited to 'server/web.go')
-rw-r--r--server/web.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/server/web.go b/server/web.go
index e238150..d55aa52 100644
--- a/server/web.go
+++ b/server/web.go
@@ -1,7 +1,9 @@
package server
import (
+ "bytes"
"crypto/rand"
+ "encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
@@ -189,6 +191,23 @@ func callbackHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int
return http.StatusFound, nil
}
+func encodeString(s string) string {
+ var buffer bytes.Buffer
+ chunkSize := 70
+ runes := []rune(base64.StdEncoding.EncodeToString([]byte(s)))
+
+ for i := 0; i < len(runes); i += chunkSize {
+ end := i + chunkSize
+ if end > len(runes) {
+ end = len(runes)
+ }
+ buffer.WriteString(string(runes[i:end]))
+ buffer.WriteString("\n")
+ }
+ buffer.WriteString(".\n")
+ return buffer.String()
+}
+
// rootHandler starts the auth process. If the client is authenticated it renders the token to the user.
func rootHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !a.isLoggedIn(w, r) {
@@ -198,6 +217,7 @@ func rootHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, er
page := struct {
Token string
}{tok.AccessToken}
+ page.Token = encodeString(page.Token)
tmpl := template.Must(template.New("token.html").Parse(templates.Token))
tmpl.Execute(w, page)