8383
8484 noauthFlag = flag .Bool ("noauth" , false , "Enables funding requests without authentication" )
8585 logFlag = flag .Int ("loglevel" , 3 , "Log level to use for Ethereum and the faucet" )
86+
87+ twitterBearerToken = flag .String ("twitter.token" , "" , "Twitter bearer token to authenticate with the twitter API" )
8688)
8789
8890var (
@@ -443,6 +445,7 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
443445 }
444446 // Retrieve the Ethereum address to fund, the requesting user and a profile picture
445447 var (
448+ id string
446449 username string
447450 avatar string
448451 address common.Address
@@ -462,11 +465,13 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
462465 }
463466 continue
464467 case strings .HasPrefix (msg .URL , "https://twitter.com/" ):
465- username , avatar , address , err = authTwitter (msg .URL )
468+ id , username , avatar , address , err = authTwitter (msg .URL , * twitterBearerToken )
466469 case strings .HasPrefix (msg .URL , "https://www.facebook.com/" ):
467470 username , avatar , address , err = authFacebook (msg .URL )
471+ id = username
468472 case * noauthFlag :
469473 username , avatar , address , err = authNoAuth (msg .URL )
474+ id = username
470475 default :
471476 //lint:ignore ST1005 This error is to be displayed in the browser
472477 err = errors .New ("Something funky happened, please open an issue at https://github.com/ethereum/go-ethereum/issues" )
@@ -486,7 +491,7 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
486491 fund bool
487492 timeout time.Time
488493 )
489- if timeout = f .timeouts [username ]; time .Now ().After (timeout ) {
494+ if timeout = f .timeouts [id ]; time .Now ().After (timeout ) {
490495 // User wasn't funded recently, create the funding transaction
491496 amount := new (big.Int ).Mul (big .NewInt (int64 (* payoutFlag )), ether )
492497 amount = new (big.Int ).Mul (amount , new (big.Int ).Exp (big .NewInt (5 ), big .NewInt (int64 (msg .Tier )), nil ))
@@ -520,7 +525,7 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
520525 timeout := time .Duration (* minutesFlag * int (math .Pow (3 , float64 (msg .Tier )))) * time .Minute
521526 grace := timeout / 288 // 24h timeout => 5m grace
522527
523- f .timeouts [username ] = time .Now ().Add (timeout - grace )
528+ f .timeouts [id ] = time .Now ().Add (timeout - grace )
524529 fund = true
525530 }
526531 f .lock .Unlock ()
@@ -684,48 +689,110 @@ func sendSuccess(conn *websocket.Conn, msg string) error {
684689}
685690
686691// authTwitter tries to authenticate a faucet request using Twitter posts, returning
687- // the username, avatar URL and Ethereum address to fund on success.
688- func authTwitter (url string ) (string , string , common.Address , error ) {
692+ // the uniqueness identifier (user id/username), username, avatar URL and Ethereum address to fund on success.
693+ func authTwitter (url string , token string ) (string , string , string , common.Address , error ) {
689694 // Ensure the user specified a meaningful URL, no fancy nonsense
690695 parts := strings .Split (url , "/" )
691696 if len (parts ) < 4 || parts [len (parts )- 2 ] != "status" {
692697 //lint:ignore ST1005 This error is to be displayed in the browser
693- return "" , "" , common.Address {}, errors .New ("Invalid Twitter status URL" )
698+ return "" , "" , "" , common.Address {}, errors .New ("Invalid Twitter status URL" )
699+ }
700+
701+ // Twitter's API isn't really friendly with direct links.
702+ // It is restricted to 300 queries / 15 minute with an app api key.
703+ // Anything more will require read only authorization from the users and that we want to avoid.
704+
705+ // If twitter bearer token is provided, use the twitter api
706+ if token != "" {
707+ return authTwitterWithToken (parts [len (parts )- 1 ], token )
694708 }
695- // Twitter's API isn't really friendly with direct links. Still, we don't
696- // want to do ask read permissions from users, so just load the public posts
709+
710+ // Twiter API token isn't provided so we just load the public posts
697711 // and scrape it for the Ethereum address and profile URL. We need to load
698712 // the mobile page though since the main page loads tweet contents via JS.
699713 url = strings .Replace (url , "https://twitter.com/" , "https://mobile.twitter.com/" , 1 )
700714
701715 res , err := http .Get (url )
702716 if err != nil {
703- return "" , "" , common.Address {}, err
717+ return "" , "" , "" , common.Address {}, err
704718 }
705719 defer res .Body .Close ()
706720
707721 // Resolve the username from the final redirect, no intermediate junk
708722 parts = strings .Split (res .Request .URL .String (), "/" )
709723 if len (parts ) < 4 || parts [len (parts )- 2 ] != "status" {
710724 //lint:ignore ST1005 This error is to be displayed in the browser
711- return "" , "" , common.Address {}, errors .New ("Invalid Twitter status URL" )
725+ return "" , "" , "" , common.Address {}, errors .New ("Invalid Twitter status URL" )
712726 }
713727 username := parts [len (parts )- 3 ]
714728
715729 body , err := ioutil .ReadAll (res .Body )
716730 if err != nil {
717- return "" , "" , common.Address {}, err
731+ return "" , "" , "" , common.Address {}, err
718732 }
719733 address := common .HexToAddress (string (regexp .MustCompile ("0x[0-9a-fA-F]{40}" ).Find (body )))
720734 if address == (common.Address {}) {
721735 //lint:ignore ST1005 This error is to be displayed in the browser
722- return "" , "" , common.Address {}, errors .New ("No Ethereum address found to fund" )
736+ return "" , "" , "" , common.Address {}, errors .New ("No Ethereum address found to fund" )
723737 }
724738 var avatar string
725739 if parts = regexp .MustCompile ("src=\" ([^\" ]+twimg.com/profile_images[^\" ]+)\" " ).FindStringSubmatch (string (body )); len (parts ) == 2 {
726740 avatar = parts [1 ]
727741 }
728- return username + "@twitter" , avatar , address , nil
742+ return username + "@twitter" , username , avatar , address , nil
743+ }
744+
745+ // authTwitterWithToken tries to authenticate a faucet request using Twitter's API, returning
746+ // the uniqueness identifier (user id/username), username, avatar URL and Ethereum address to fund on success.
747+ func authTwitterWithToken (tweetID string , token string ) (string , string , string , common.Address , error ) {
748+ // Strip any query parameters from the tweet id
749+ sanitizedTweetID := strings .Split (tweetID , "?" )[0 ]
750+
751+ // Ensure numeric tweetID
752+ if ! regexp .MustCompile ("^[0-9]+$" ).MatchString (sanitizedTweetID ) {
753+ return "" , "" , "" , common.Address {}, errors .New ("Invalid Tweet URL" )
754+ }
755+
756+ // Query the tweet details from Twitter
757+ url := fmt .Sprintf ("https://api.twitter.com/2/tweets/%s?expansions=author_id&user.fields=profile_image_url" , sanitizedTweetID )
758+ req , err := http .NewRequest ("GET" , url , nil )
759+ if err != nil {
760+ return "" , "" , "" , common.Address {}, err
761+ }
762+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , token ))
763+ res , err := http .DefaultClient .Do (req )
764+ if err != nil {
765+ return "" , "" , "" , common.Address {}, err
766+ }
767+ defer res .Body .Close ()
768+
769+ var result struct {
770+ Data struct {
771+ AuthorID string `json:"author_id"`
772+ ID string `json:"id"`
773+ Text string `json:"text"`
774+ } `json:"data"`
775+ Includes struct {
776+ Users []struct {
777+ ProfileImageURL string `json:"profile_image_url"`
778+ Username string `json:"username"`
779+ ID string `json:"id"`
780+ Name string `json:"name"`
781+ } `json:"users"`
782+ } `json:"includes"`
783+ }
784+
785+ err = json .NewDecoder (res .Body ).Decode (& result )
786+ if err != nil {
787+ return "" , "" , "" , common.Address {}, err
788+ }
789+
790+ address := common .HexToAddress (regexp .MustCompile ("0x[0-9a-fA-F]{40}" ).FindString (result .Data .Text ))
791+ if address == (common.Address {}) {
792+ //lint:ignore ST1005 This error is to be displayed in the browser
793+ return "" , "" , "" , common.Address {}, errors .New ("No Ethereum address found to fund" )
794+ }
795+ return result .Data .AuthorID + "@twitter" , result .Includes .Users [0 ].Username , result .Includes .Users [0 ].ProfileImageURL , address , nil
729796}
730797
731798// authFacebook tries to authenticate a faucet request using Facebook posts,
0 commit comments