commit 1e0127d3e9ccab1bdc06feac1590231b0a2040fb
parent 1a0a49ac82af8ef06e8d036eeb2ea56e89404250
Author: MTRNord <mtrnord1@gmail.com>
Date: Fri, 30 Mar 2018 22:11:04 +0200
Use Host Header for callback
Diffstat:
3 files changed, 45 insertions(+), 13 deletions(-)
diff --git a/api/login/twitter.go b/api/login/twitter.go
@@ -1,11 +1,17 @@
package login
import (
+ "github.com/dghubble/gologin"
+ libLogin "github.com/dghubble/gologin/oauth1"
+ oauth1Login "github.com/dghubble/gologin/oauth1"
"github.com/dghubble/gologin/twitter"
+ "github.com/dghubble/oauth1"
"github.com/dghubble/sessions"
"net/http"
)
+var TConfig *oauth1.Config
+
const (
sessionName = "ssn-app"
sessionSecret = "example cookie signing secret"
@@ -33,3 +39,39 @@ func IssueSession() http.Handler {
}
return http.HandlerFunc(fn)
}
+
+// LoginHandler handles Twitter login requests by obtaining a request token and
+// redirecting to the authorization URL.
+func LoginHandler(config *oauth1.Config, failure http.Handler) http.Handler {
+ // oauth1.LoginHandler -> oauth1.AuthRedirectHandler
+ success := AuthRedirectHandler(config, failure)
+ return oauth1Login.LoginHandler(config, success, failure)
+}
+
+// AuthRedirectHandler reads the request token from the ctx and redirects
+// to the authorization URL.
+func AuthRedirectHandler(config *oauth1.Config, failure http.Handler) http.Handler {
+ if failure == nil {
+ failure = gologin.DefaultFailureHandler
+ }
+ fn := func(w http.ResponseWriter, req *http.Request) {
+ domain := req.Header.Get("Host")
+ config.CallbackURL = "https://" + domain + "/login/twitter/callback"
+ TConfig = config
+ ctx := req.Context()
+ requestToken, _, err := libLogin.RequestTokenFromContext(ctx)
+ if err != nil {
+ ctx = gologin.WithError(ctx, err)
+ failure.ServeHTTP(w, req.WithContext(ctx))
+ return
+ }
+ authorizationURL, err := config.AuthorizationURL(requestToken)
+ if err != nil {
+ ctx = gologin.WithError(ctx, err)
+ failure.ServeHTTP(w, req.WithContext(ctx))
+ return
+ }
+ http.Redirect(w, req, authorizationURL.String(), http.StatusFound)
+ }
+ return http.HandlerFunc(fn)
+}
diff --git a/config/config.go b/config/config.go
@@ -12,12 +12,6 @@ var configOnce sync.Once
type Config struct {
TwitterConfig `yaml:"twitter"`
- ServerConfig `yaml:"server"`
-}
-
-type ServerConfig struct {
- Domain string `yaml:"domain"`
- HTTPS bool `yaml:"https"`
}
type TwitterConfig struct {
diff --git a/main.go b/main.go
@@ -25,16 +25,12 @@ func main() {
api.Login(configData.ConsumerKey, configData.ConsumerSecret)
fmt.Println("Logged in!")
- TlConfig := &oauth1.Config{
+ login.TConfig = &oauth1.Config{
ConsumerKey: configData.ConsumerKey,
ConsumerSecret: configData.ConsumerSecret,
Endpoint: twitterOAuth1.AuthorizeEndpoint,
}
- if configData.HTTPS {
- TlConfig.CallbackURL = "https://" + configData.Domain + "/login/twitter/callback"
- }
-
r := mux.NewRouter()
r.HandleFunc("/papers", web_api.Papers).Methods("GET", "POST")
r.HandleFunc("/paper/{uuid}", web_api.PaperFunc).Methods("GET")
@@ -43,8 +39,8 @@ func main() {
p.HandleFunc("/yesterday", web_api.Yesterday).Methods("GET")
l := r.PathPrefix("/login").Subrouter()
- l.Handle("/twitter", tLogin.LoginHandler(TlConfig, nil))
- l.Handle("/twitter/callback", tLogin.CallbackHandler(TlConfig, login.IssueSession(), nil))
+ l.Handle("/twitter", login.LoginHandler(login.TConfig, nil))
+ l.Handle("/twitter/callback", tLogin.CallbackHandler(login.TConfig, login.IssueSession(), nil))
// cors.Default() setup the middleware with default options being
// all origins accepted with simple methods (GET, POST). See