Skip to content
Snippets Groups Projects
Commit 76fc7c9f authored by Kevin Lyda's avatar Kevin Lyda :speech_balloon:
Browse files

Provide a way to offer chunked tokens.

parent 6c306e58
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,14 @@ package main
import (
"bufio"
"bytes"
"fmt"
"log"
"net"
"os"
"os/user"
"path"
"strings"
"time"
"github.com/nsheridan/cashier/client"
......@@ -49,12 +51,16 @@ func main() {
}
fmt.Print("Enter token: ")
var token string
fmt.Scanln(&token)
scanner := bufio.NewScanner(os.Stdin)
var buffer bytes.Buffer
for scanner.Scan(); strings.HasSuffix(scanner.Text(), "+++"); scanner.Scan() {
buffer.WriteString(scanner.Text()[:len(scanner.Text())-4])
}
buffer.WriteString(scanner.Text())
token := buffer.String()
var message string
fmt.Print("Enter message: ")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
message = scanner.Text()
}
......
package server
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
......@@ -181,6 +182,29 @@ func callbackHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int
return http.StatusFound, nil
}
func chunkString(s string, chunkSize int) string {
if len(s) <= chunkSize {
return s
}
var buffer bytes.Buffer
runes := []rune(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")
}
chunks := buffer.String()
if len(chunks) > 0 {
chunks = chunks[:len(chunks)-4]
}
return chunks
}
// 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) {
......@@ -190,6 +214,7 @@ func rootHandler(a *appContext, w http.ResponseWriter, r *http.Request) (int, er
page := struct {
Token string
}{tok.AccessToken}
page.Token = chunkString(page.Token, 250)
tmpl := template.Must(template.New("token.html").Parse(templates.Token))
tmpl.Execute(w, page)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment