<commit_before>package controllers

import (
	"fmt"
	"net/http"
	"os"
	"time"

	"google.golang.org/appengine"

	"github.com/mrjones/oauth"
	"github.com/otiai10/chant/provider"
	"github.com/otiai10/chant/server/models"
	"github.com/otiai10/marmoset"
)

// Auth handles starting point for OAuth communication.
// This endpoint is hit intentionally from "/login"
func Auth(w http.ResponseWriter, r *http.Request) {

	// {{{ AppEngine Specific code
	ctx := appengine.NewContext(r)
	provider.SharedInstance.SetContext(ctx)
	// }}}

	// XXX: We can't use r.URL.Scheme/Host to create callback URL,
	//      because "goapp" proxy server remove them.
	callback := fmt.Sprintf("%s/auth/callback", r.Header.Get("Origin"))
	requestToken, url, err := provider.SharedInstance.GetRequestTokenAndUrl(callback)

	if err != nil {
		marmoset.Render(w).HTML("login", marmoset.P{
			"error": err,
		})
		return
	}

	// Store as Cookies
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_token",
		Value: requestToken.Token, Path: "/",
		Expires: time.Now().Add(1 * time.Minute),
	})
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_secret",
		Value: requestToken.Secret, Path: "/",
		Expires: time.Now().Add(1 * time.Minute),
	})

	http.Redirect(w, r, url, http.StatusFound)
}

// AuthCallback handles callback request from Twitter,
// after users accept authentication request by CHANT.
func AuthCallback(w http.ResponseWriter, r *http.Request) {

	// {{{ AppEngine Specific code
	ctx := appengine.NewContext(r)
	provider.SharedInstance.SetContext(ctx)
	// }}}

	verifier := r.FormValue("oauth_verifier")
	if verifier == "" {
		panic(fmt.Errorf("TODO: Error Handling: %s", "No `oauth_verifier given to callback`"))
	}

	token, err := r.Cookie("chant_twitter_request_token")
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	secret, err := r.Cookie("chant_twitter_request_secret")
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	// Restore RequestToken from Cookies
	rtoken := &oauth.RequestToken{Token: token.Value, Secret: secret.Value}

	// Request AccessToken by this RequestToken and verifier just granted
	accesstoken, err := provider.SharedInstance.AuthorizeToken(rtoken, verifier)
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	// Flush Unnecessary Cookies
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_token",
		Value: "", Path: "/", Expires: time.Time{},
	})
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_secret",
		Value: "", Path: "/", Expires: time.Time{},
	})<commit_msg>Pass valid Expires to delete unnecessary cookies<commit_after>package controllers

import (
	"fmt"
	"net/http"
	"os"
	"time"

	"google.golang.org/appengine"

	"github.com/mrjones/oauth"
	"github.com/otiai10/chant/provider"
	"github.com/otiai10/chant/server/models"
	"github.com/otiai10/marmoset"
)

// Auth handles starting point for OAuth communication.
// This endpoint is hit intentionally from "/login"
func Auth(w http.ResponseWriter, r *http.Request) {

	// {{{ AppEngine Specific code
	ctx := appengine.NewContext(r)
	provider.SharedInstance.SetContext(ctx)
	// }}}

	// XXX: We can't use r.URL.Scheme/Host to create callback URL,
	//      because "goapp" proxy server remove them.
	callback := fmt.Sprintf("%s/auth/callback", r.Header.Get("Origin"))
	requestToken, url, err := provider.SharedInstance.GetRequestTokenAndUrl(callback)

	if err != nil {
		marmoset.Render(w).HTML("login", marmoset.P{
			"error": err,
		})
		return
	}

	// Store as Cookies
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_token",
		Value: requestToken.Token, Path: "/",
		Expires: time.Now().Add(1 * time.Minute),
	})
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_secret",
		Value: requestToken.Secret, Path: "/",
		Expires: time.Now().Add(1 * time.Minute),
	})

	http.Redirect(w, r, url, http.StatusFound)
}

// AuthCallback handles callback request from Twitter,
// after users accept authentication request by CHANT.
func AuthCallback(w http.ResponseWriter, r *http.Request) {

	// {{{ AppEngine Specific code
	ctx := appengine.NewContext(r)
	provider.SharedInstance.SetContext(ctx)
	// }}}

	verifier := r.FormValue("oauth_verifier")
	if verifier == "" {
		panic(fmt.Errorf("TODO: Error Handling: %s", "No `oauth_verifier given to callback`"))
	}

	token, err := r.Cookie("chant_twitter_request_token")
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	secret, err := r.Cookie("chant_twitter_request_secret")
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	// Restore RequestToken from Cookies
	rtoken := &oauth.RequestToken{Token: token.Value, Secret: secret.Value}

	// Request AccessToken by this RequestToken and verifier just granted
	accesstoken, err := provider.SharedInstance.AuthorizeToken(rtoken, verifier)
	if err != nil {
		panic(fmt.Errorf("TODO: Error Handling: %v", err))
	}

	// Flush Unnecessary Cookies
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_token",
		Value: "", Path: "/", Expires: time.Now(),
	})
	http.SetCookie(w, &http.Cookie{
		Name:  "chant_twitter_request_secret",
		Value: "", Path: "/", Expires: time.Now(),
	})
{'fraction_non_alphanumeric': 0.152938938558113, 'fraction_numerical': 0.003043560966330607, 'mean_word_length': 4.392820512820513, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3071265', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1811, 'n_words': 410}