bot: enable simple auth workflow
This isn't good enough yet. Needs: * to save the authorization for restarts * more permission scopes * permanent auth instead of an hour's auth
This commit is contained in:
parent
edf4900ea6
commit
d11b2382ef
|
@ -206,3 +206,4 @@ $RECYCLE.BIN/
|
|||
/beebot
|
||||
/cmd/beebot/beebot
|
||||
beebot.db
|
||||
run.sh
|
||||
|
|
|
@ -2,12 +2,66 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/jzelinskie/geddit"
|
||||
)
|
||||
|
||||
const version = 1.0
|
||||
|
||||
var clientID = flag.String("id", "", "Client ID")
|
||||
var clientSecret = flag.String("secret", "", "Client Secret")
|
||||
var baseAddr = flag.String("url", "127.0.0.1:9595", "Base address")
|
||||
|
||||
var o *geddit.OAuthSession
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log.Printf("BeeBot v%.2f", version)
|
||||
|
||||
var err error
|
||||
|
||||
o, err = geddit.NewOAuthSession(
|
||||
*clientID,
|
||||
*clientSecret,
|
||||
"BeeBot",
|
||||
fmt.Sprintf("http://%s/cb", *baseAddr),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(1, err)
|
||||
}
|
||||
|
||||
state := strconv.Itoa(rand.Intn(10000) * 1000)
|
||||
url := o.AuthCodeURL(state, []string{"identity read edit"})
|
||||
log.Printf("Visit %s to obtain auth code", url)
|
||||
|
||||
http.HandleFunc("/cb", mkCb(state))
|
||||
log.Fatal(http.ListenAndServe(*baseAddr, nil))
|
||||
|
||||
}
|
||||
|
||||
func mkCb(startState string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
state := r.URL.Query().Get("state")
|
||||
code := r.URL.Query().Get("code")
|
||||
if state != startState {
|
||||
log.Fatalf("Unexpected state, %s != %s", startState, state)
|
||||
}
|
||||
|
||||
err := o.CodeAuth(code)
|
||||
if err != nil {
|
||||
log.Fatal(2, err)
|
||||
}
|
||||
|
||||
me, err := o.AboutRedditor("_beebot_")
|
||||
if err != nil {
|
||||
log.Fatal(3, err)
|
||||
}
|
||||
|
||||
log.Printf("%+v", me)
|
||||
}
|
||||
}
|
||||
|
|
8
go.mod
8
go.mod
|
@ -1,3 +1,11 @@
|
|||
module code.chrissexton.org/cws/BeeBot
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/beefsack/go-rate v0.0.0-20180408011153-efa7637bb9b6 // indirect
|
||||
github.com/google/go-querystring v1.0.0 // indirect
|
||||
github.com/jzelinskie/geddit v0.0.0-20190913104144-95ef6806b073
|
||||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
|
||||
)
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/beefsack/go-rate v0.0.0-20180408011153-efa7637bb9b6 h1:KXlsf+qt/X5ttPGEjR0tPH1xaWWoKBEg9Q1THAj2h3I=
|
||||
github.com/beefsack/go-rate v0.0.0-20180408011153-efa7637bb9b6/go.mod h1:6YNgTHLutezwnBvyneBbwvB8C82y3dcoOj5EQJIdGXA=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/jzelinskie/geddit v0.0.0-20190913104144-95ef6806b073 h1:5SmVkj0GZ8GU4eUF6JOhwZj4GeCxPphxTdZK07R5Q1U=
|
||||
github.com/jzelinskie/geddit v0.0.0-20190913104144-95ef6806b073/go.mod h1:KiUhpHWSO6xCSPYKhRXa1LDLtbxZKaFH4NINTP3Lm2Q=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
|
||||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
Loading…
Reference in New Issue