Initial commit: Skeleton for client communication

master
Hugo Thunnissen 4 years ago
commit 6a7e6cef05

1
.gitignore vendored

@ -0,0 +1 @@
/generic-mastodon-authenticator

@ -0,0 +1,15 @@
package main
type AuthRequest struct {
Client *AuthRequestClient
Instance string
ID string
}
func (r *AuthRequest) SetId(ID string) error {
r.ID = ID
err := r.Client.PropagateID(ID)
return err
}

@ -0,0 +1,85 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
)
const (
MSG_TYPE_AUTH string = "auth"
MSG_TYPE_SET_ID string = "set-id"
)
type AuthRequestClient struct {
conn *websocket.Conn
}
type AuthRequestProtocolMessage struct {
MessageType string `json:"type"`
Parameters map[string]string
}
func (c *AuthRequestClient) ReceiveRequest() (*AuthRequest, error) {
message, err := c.receiveProtocolMessage()
if err != nil {
return nil, err
}
if message.MessageType != MSG_TYPE_AUTH {
return nil, errors.New(
fmt.Sprintf(
"Wrong protocol message type, expected message of type \"%s\", got \"%s\"",
MSG_TYPE_AUTH,
message.MessageType,
),
)
}
instance, ok := message.Parameters["instance"]
if !ok {
return nil, errors.New("No \"instance\" parameter included in auth request parameters")
}
r := &AuthRequest{
Client: c,
Instance: instance,
}
return r, nil
}
func (c *AuthRequestClient) receiveProtocolMessage() (*AuthRequestProtocolMessage, error) {
_, message, err := c.conn.ReadMessage()
if err != nil {
return nil, err
}
var protocolMessage *AuthRequestProtocolMessage
err = json.Unmarshal(message, protocolMessage)
if err != nil {
return nil, err
}
return protocolMessage, nil
}
func (c *AuthRequestClient) PropagateID(ID string) error {
message := &AuthRequestProtocolMessage{
MessageType: MSG_TYPE_SET_ID,
Parameters: map[string]string{
"id": ID,
},
}
err := c.conn.WriteJSON(message)
return err
}

@ -0,0 +1,6 @@
package main
type AuthToken struct {
RequestID string
Token string
}

@ -0,0 +1,53 @@
package main
// Queue for pending authentication requests
import (
"log"
"math/rand"
"time"
"github.com/oklog/ulid"
)
type AuthenticationQueue struct {
requestMap map[string]*AuthRequest
requestChan chan *AuthRequest
tokenChan chan *AuthToken
}
func (q *AuthenticationQueue) QueueRequest(r *AuthRequest) {
q.requestChan <- r
}
// Should be executed by the goroutine that watches
// AuthenticationQueue.requestChan to be thread safe
func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error {
t := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
ulidID := ulid.MustNew(ulid.Timestamp(t), entropy)
ID := ulidID.String()
err := r.SetId(ID)
if err != nil {
return err
}
q.requestMap[ID] = r
log.Println("Queued request by id: ", ID)
return nil
}
func (q *AuthenticationQueue) startRequestChanListener() {
go func() {
for {
var req *AuthRequest = <-q.requestChan
q.queueRequest(req)
}
}()
}

@ -0,0 +1,13 @@
module code-knockers.com/hugo/generic-mastodon-authenticator
go 1.13
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gorilla/websocket v1.4.1
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect
github.com/oklog/ulid v1.3.1
github.com/valyala/fasttemplate v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba // indirect
)

@ -0,0 +1,43 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4=
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba h1:9bFeDpN3gTqNanMVqNcoR/pJQuP5uroC3t1D7eXozTE=
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

@ -0,0 +1,58 @@
package main
// Small generic oauth authentication server to retrieve oauth tokens
// from mastodon instances.
import (
"github.com/gorilla/websocket"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// github.com/oklog/ulid
var (
upgrader = websocket.Upgrader{}
)
type App struct {
AuthQueue *AuthenticationQueue
}
func (a *App) authRequestWebSocket(c echo.Context) error {
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
client := &AuthRequestClient{
conn: ws,
}
req, err := client.ReceiveRequest()
if err != nil {
return err
}
a.AuthQueue.QueueRequest(req)
return nil
}
func main() {
q := &AuthenticationQueue{}
q.startRequestChanListener()
a := &App{
AuthQueue: q,
}
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", "../public")
e.GET("/ws", a.authRequestWebSocket)
e.Logger.Fatal(e.Start(":1323"))
}
Loading…
Cancel
Save