summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-04-10 12:27:16 -0400
committerCara Salter <cara@devcara.com>2023-04-10 12:27:16 -0400
commitd68f829531f055a5837310b7b6c69d242af6d240 (patch)
treef408b8b397a0e0e9b225602ab1f03f022a475ee9
download142bot-web-d68f829531f055a5837310b7b6c69d242af6d240.tar.gz
142bot-web-d68f829531f055a5837310b7b6c69d242af6d240.zip
initial commit
-rw-r--r--.gitignore30
-rw-r--r--go.mod21
-rw-r--r--go.sum52
-rw-r--r--main.go114
4 files changed, 217 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..70fa4df
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,30 @@
+# Created by https://www.toptal.com/developers/gitignore/api/dotenv,go
+# Edit at https://www.toptal.com/developers/gitignore?templates=dotenv,go
+
+### dotenv ###
+.env
+
+### Go ###
+# If you prefer the allow list template instead of the deny list, see community template:
+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
+#
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+
+# End of https://www.toptal.com/developers/gitignore/api/dotenv,go
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..5b9d441
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,21 @@
+module git.devcara.com/142bot-web
+
+go 1.20
+
+require (
+ github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
+ github.com/joho/godotenv v1.5.1
+ golang.org/x/oauth2 v0.7.0
+)
+
+require (
+ github.com/fatih/color v1.10.0 // indirect
+ github.com/golang/protobuf v1.5.2 // indirect
+ github.com/lib/pq v1.10.7 // indirect
+ github.com/mattn/go-colorable v0.1.8 // indirect
+ github.com/mattn/go-isatty v0.0.12 // indirect
+ golang.org/x/net v0.9.0 // indirect
+ golang.org/x/sys v0.7.0 // indirect
+ google.golang.org/appengine v1.6.7 // indirect
+ google.golang.org/protobuf v1.28.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..311fe48
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,52 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
+github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
+github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1 h1:KkEP5qFmoabNkkRD+Fz4O3GHDwpsecWoZ2J45Luw3n0=
+github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1/go.mod h1:sGBOiXpXj5KPWwXHhzyGJBe4+UC8NpIfQf7pTlTwMYI=
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
+github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
+github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
+github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
+github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
+github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
+github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
+github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
+github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
+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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
+golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
+golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
+golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g=
+golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
+golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
+google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
+google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
+google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
+google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
+google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..c51a301
--- /dev/null
+++ b/main.go
@@ -0,0 +1,114 @@
+package main
+
+import (
+ "context"
+ "database/sql"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "os"
+
+ _ "github.com/lib/pq"
+
+ "github.com/gleich/logoru"
+ "github.com/joho/godotenv"
+ "golang.org/x/oauth2"
+ "golang.org/x/oauth2/endpoints"
+)
+
+var spotifyConfig oauth2.Config
+
+var db *sql.DB
+
+func Success(w http.ResponseWriter, r *http.Request) {
+ io.WriteString(w, "Success!")
+}
+
+func LandingPage(w http.ResponseWriter, r *http.Request) {
+ logoru.Info("Starting auth process")
+ spotifyUrl := spotifyConfig.AuthCodeURL("zyx")
+ http.Redirect(w, r, spotifyUrl, http.StatusFound)
+}
+
+func Privacy(w http.ResponseWriter, r *http.Request) {
+ io.WriteString(w, "No.")
+}
+
+func SpotifyAuth(w http.ResponseWriter, r *http.Request) {
+ r.ParseForm()
+ state := r.Form.Get("state")
+ if state != "zyx" {
+ http.Error(w, "Invalid State", http.StatusBadRequest)
+ return
+ }
+
+ code := r.Form.Get("code")
+ if code == "" {
+ http.Error(w, "Code not found", http.StatusBadRequest)
+ return
+ }
+
+ token, err := spotifyConfig.Exchange(context.Background(), code)
+ if err != nil {
+ http.Error(w, "Couldn't exchange code", http.StatusInternalServerError)
+ return
+ }
+
+ var bearer = "Bearer " + token.AccessToken
+
+ req, _ := http.NewRequest("GET", "https://api.spotify.com/v1/me", nil)
+
+ req.Header.Add("Authorization", bearer)
+
+ client := &http.Client{}
+
+ resp, err := client.Do(req)
+ if err != nil {
+ logoru.Critical("Couldn't get API information")
+ http.Error(w, "", http.StatusInternalServerError)
+ return
+ }
+ var generic map[string]interface{}
+ err = json.NewDecoder(resp.Body).Decode(&generic)
+ if err != nil {
+ return
+ }
+ logoru.Debug(generic)
+
+ _, err = db.Exec("INSERT INTO spotify (spotify_username, spotify_token, spotify_token_expires, spotify_refresh_token) VALUES ($1, $2, $3, $4)", generic["display_name"], token.AccessToken, token.Expiry, token.RefreshToken)
+
+ if err != nil {
+ logoru.Error(err.Error())
+ return
+ }
+}
+
+func main() {
+ err := godotenv.Load()
+ if err != nil {
+ logoru.Critical("Could not load .env")
+ }
+
+ spotifyConfig = oauth2.Config{
+ ClientID: os.Getenv("SPOTIFY_CLIENT_ID"),
+ ClientSecret: os.Getenv("SPOTIFY_CLIENT_SECRET"),
+ Scopes: []string{"user-modify-playback-state", "user-read-email"},
+ RedirectURL: fmt.Sprintf("%s/oauth/spotify", os.Getenv("BASE_URL")),
+ Endpoint: endpoints.Spotify,
+ }
+
+ db, err = sql.Open("postgres", os.Getenv("POSTGRES_URL"))
+ if err != nil {
+ logoru.Critical("Could not open database connection")
+ return
+ }
+
+ http.HandleFunc("/", LandingPage)
+ http.HandleFunc("/oauth/spotify", SpotifyAuth)
+ http.HandleFunc("/success", Success)
+ http.HandleFunc("/privay", Privacy)
+
+ logoru.Info("Starting client")
+ logoru.Critical(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("LISTEN_PORT")), nil))
+}