Initial discord remote auth support.
It works, but nothing is persisted yet as the user object needs some tweaking especially the database side.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/beeper/discord/consts"
|
||||
"gitlab.com/beeper/discord/remoteauth"
|
||||
"gitlab.com/beeper/discord/version"
|
||||
)
|
||||
|
||||
@@ -45,6 +47,7 @@ type commands struct {
|
||||
globals
|
||||
|
||||
Help helpCmd `kong:"cmd,help='Displays this message.'"`
|
||||
Login loginCmd `kong:"cmd,help='Log in to Discord.'"`
|
||||
Version versionCmd `kong:"cmd,help='Displays the version of the bridge.'"`
|
||||
}
|
||||
|
||||
@@ -79,3 +82,53 @@ func (c *versionCmd) Run(g *globals) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type loginCmd struct{}
|
||||
|
||||
func (l *loginCmd) Run(g *globals) error {
|
||||
client, err := remoteauth.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
qrChan := make(chan string)
|
||||
doneChan := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
code := <-qrChan
|
||||
|
||||
_, err := g.user.sendQRCode(g.bot, g.roomID, code)
|
||||
if err != nil {
|
||||
fmt.Fprintln(g.context.Stdout, "failed to generate the qrcode")
|
||||
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
if err := client.Dial(ctx, qrChan, doneChan); err != nil {
|
||||
close(qrChan)
|
||||
close(doneChan)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
<-doneChan
|
||||
|
||||
user, err := client.Result()
|
||||
if err != nil {
|
||||
fmt.Printfln(g.context.Stdout, "failed to log in")
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
g.user.User.ID = user.UserID
|
||||
g.user.User.Discriminator = user.Discriminator
|
||||
g.user.User.Username = user.Username
|
||||
|
||||
g.handler.log.Warnln("users:", user)
|
||||
g.handler.log.Warnln("err:", err)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"github.com/skip2/go-qrcode"
|
||||
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
"maunium.net/go/mautrix/appservice"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/beeper/discord/database"
|
||||
@@ -91,3 +95,43 @@ func (u *User) SetManagementRoom(roomID id.RoomID) {
|
||||
func (u *User) HasSession() bool {
|
||||
return u.User.Session != nil
|
||||
}
|
||||
|
||||
func (u *User) sendQRCode(bot *appservice.IntentAPI, roomID id.RoomID, code string) (id.EventID, error) {
|
||||
url, err := u.uploadQRCode(code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
content := event.MessageEventContent{
|
||||
MsgType: event.MsgImage,
|
||||
Body: code,
|
||||
URL: url.CUString(),
|
||||
}
|
||||
|
||||
resp, err := bot.SendMessageEvent(roomID, event.EventMessage, &content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return resp.EventID, nil
|
||||
}
|
||||
|
||||
func (u *User) uploadQRCode(code string) (id.ContentURI, error) {
|
||||
qrCode, err := qrcode.Encode(code, qrcode.Low, 256)
|
||||
if err != nil {
|
||||
u.log.Errorln("Failed to encode QR code:", err)
|
||||
|
||||
return id.ContentURI{}, err
|
||||
}
|
||||
|
||||
bot := u.bridge.as.BotClient()
|
||||
|
||||
resp, err := bot.UploadBytes(qrCode, "image/png")
|
||||
if err != nil {
|
||||
u.log.Errorln("Failed to upload QR code:", err)
|
||||
|
||||
return id.ContentURI{}, err
|
||||
}
|
||||
|
||||
return resp.ContentURI, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user