socialnetworknews-api

git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-api.git
Log | Files | Refs | LICENSE

commit 1a0a49ac82af8ef06e8d036eeb2ea56e89404250
parent a5cf74d21b8fa23e665266c63bd73824a01b3ed9
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri, 30 Mar 2018 21:42:02 +0200

Implement basic Login via Twitter (WIP)

Diffstat:
Aapi/login/twitter.go | 35+++++++++++++++++++++++++++++++++++
Mapi/webserver.go | 2+-
Mconfig/config.go | 6++++++
Mmain.go | 19+++++++++++++++++++
Mtwitter/twitter.go | 3---
5 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/api/login/twitter.go b/api/login/twitter.go @@ -0,0 +1,35 @@ +package login + +import ( + "github.com/dghubble/gologin/twitter" + "github.com/dghubble/sessions" + "net/http" +) + +const ( + sessionName = "ssn-app" + sessionSecret = "example cookie signing secret" + sessionUserKey = "twitterID" +) + +// sessionStore encodes and decodes session data stored in signed cookies +var sessionStore = sessions.NewCookieStore([]byte(sessionSecret), nil) + +// issueSession issues a cookie session after successful Twitter login +func IssueSession() http.Handler { + fn := func(w http.ResponseWriter, req *http.Request) { + ctx := req.Context() + twitterUser, err := twitter.UserFromContext(ctx) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // 2. Implement a success handler to issue some form of session + session := sessionStore.New(sessionName) + session.Values[sessionUserKey] = twitterUser.ID + session.Save(w) + domain := req.Header.Get("Host") + http.Redirect(w, req, domain, http.StatusFound) + } + return http.HandlerFunc(fn) +} diff --git a/api/webserver.go b/api/webserver.go @@ -34,7 +34,7 @@ func (e *TweetsError) StatusCode() int { func Yesterday(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - //TODO Use Database or File Structure (probably File Structure) + // TODO Use Database or File Structure (probably File Structure) uuidVar := vars["uuid"] fmt.Println("UUID: ", uuidVar) diff --git a/config/config.go b/config/config.go @@ -12,6 +12,12 @@ 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 @@ -3,8 +3,12 @@ package main import ( "fmt" web_api "github.com/SocialNetworkNews/SocialNetworkNews_API/api" + "github.com/SocialNetworkNews/SocialNetworkNews_API/api/login" "github.com/SocialNetworkNews/SocialNetworkNews_API/config" "github.com/SocialNetworkNews/SocialNetworkNews_API/twitter" + tLogin "github.com/dghubble/gologin/twitter" + "github.com/dghubble/oauth1" + twitterOAuth1 "github.com/dghubble/oauth1/twitter" "github.com/gorilla/mux" "github.com/rs/cors" "log" @@ -21,12 +25,27 @@ func main() { api.Login(configData.ConsumerKey, configData.ConsumerSecret) fmt.Println("Logged in!") + TlConfig := &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") p := r.PathPrefix("/paper/{uuid}").Subrouter() 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)) + // cors.Default() setup the middleware with default options being // all origins accepted with simple methods (GET, POST). See // documentation below for more options. diff --git a/twitter/twitter.go b/twitter/twitter.go @@ -226,9 +226,6 @@ func (t *TwitterAPI) GetTweets(tweets []int64) ([]byte, error) { } else { retweets[t.RetweetedStatus.IdStr] = true } - if tweetsSlice[t.RetweetedStatus.IdStr] { - continue - } } else { JT.Retweet = false }