<commit_before>package login

// What I cloned from ...
// https://github.com/douglasmakey/oauth2-example/blob/master/handlers/oauth_google.go

import(
	"context"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
)

var(
	// GoogleClientID and GoogleClientSecret are credentials you'll get
	// when you setup OAuth2 logins on the google side. You *must*
	// specify these two values.
	GoogleClientID string
	GoogleClientSecret string
)

const(
	GoogleOAuthEmailScope = "https://www.googleapis.com/auth/userinfo.email" // WTF to get this
	oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="
)

type GoogleOauth2 struct {
	oauth2.Config
}
func (g GoogleOauth2)Name() string { return "google" }

// {{{ NewGoogleOauth2

func NewGoogleOauth2() GoogleOauth2 {
	g := GoogleOauth2{
		Config: oauth2.Config{
			ClientID:     GoogleClientID,     // config.Get("google.oauth2.appid"),
			ClientSecret: GoogleClientSecret, // config.Get("google.oauth2.secret"),
			Endpoint:     google.Endpoint,
			Scopes:       []string{GoogleOAuthEmailScope},
		},
	}

	// Build this from some package variables
	g.Config.RedirectURL = Host + getCallbackRelativeUrl(g)

	return g
}

// }}}

// {{{ goauth2.GetLoginUrl

func (goauth2 GoogleOauth2)GetLoginUrl(w http.ResponseWriter, r *http.Request) string {
	// AuthCodeURL is a token to protect the user from CSRF attacks. It
	// is also attached to the response as a cookie.
	oauthState := generateStateOauthCookie(w)
	return goauth2.Config.AuthCodeURL(oauthState)
}

// }}}
// {{{ goauth2.GetLogoutUrl

func (goauth2 GoogleOauth2)GetLogoutUrl(w http.ResponseWriter, r *http.Request) string {
	loginUrl := makeUrlAbsolute(r, "/")

	args1 := url.Values{}
	args1.Add("continue", loginUrl)

	args2 := url.Values{}
	args2.Add("continue", "https://appengine.google.com/_ah/logout?" + args1.Encode())
	
	// This weird thing bounces you through a "are you sure you want to redirect" notice,
	// which is fine. You can't just redirect anywhere, it has to go to a google url.
	return "https://www.google.com/accounts/Logout?" + args2.Encode()
}

// }}}
// {{{ goauth2.CallbackToEmail

func (goauth2 GoogleOauth2)CallbackToEmail(r *http.Request) (string, error) {
	// Read oauthState from Cookie
	if oauthState,err := r.Cookie("oauthstate"); err != nil {
		return "", fmt.Errorf("no oauth google cookie: %v", err)
	} else if r.FormValue("state") != oauthState.Value {
		return "", fmt.Errorf("invalid oauth google state")
	}

	email,err := goauth2.getEmailFromGoogle(r.FormValue("code"))
	return email,err
}
<commit_msg>Start flagging these problems<commit_after>package login

// What I cloned from ...
// https://github.com/douglasmakey/oauth2-example/blob/master/handlers/oauth_google.go

import(
	"context"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"time"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
)

var(
	// GoogleClientID and GoogleClientSecret are credentials you'll get
	// when you setup OAuth2 logins on the google side. You *must*
	// specify these two values.
	GoogleClientID string
	GoogleClientSecret string
)

const(
	GoogleOAuthEmailScope = "https://www.googleapis.com/auth/userinfo.email" // WTF to get this
	oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="
)

type GoogleOauth2 struct {
	oauth2.Config
}
func (g GoogleOauth2)Name() string { return "google" }

// {{{ NewGoogleOauth2

func NewGoogleOauth2() GoogleOauth2 {
	g := GoogleOauth2{
		Config: oauth2.Config{
			ClientID:     GoogleClientID,     // config.Get("google.oauth2.appid"),
			ClientSecret: GoogleClientSecret, // config.Get("google.oauth2.secret"),
			Endpoint:     google.Endpoint,
			Scopes:       []string{GoogleOAuthEmailScope},
		},
	}

	// Build this from some package variables
	g.Config.RedirectURL = Host + getCallbackRelativeUrl(g)

	return g
}

// }}}

// {{{ goauth2.GetLoginUrl

func (goauth2 GoogleOauth2)GetLoginUrl(w http.ResponseWriter, r *http.Request) string {
	// AuthCodeURL is a token to protect the user from CSRF attacks. It
	// is also attached to the response as a cookie.
	oauthState := generateStateOauthCookie(w)
	return goauth2.Config.AuthCodeURL(oauthState)
}

// }}}
// {{{ goauth2.GetLogoutUrl

func (goauth2 GoogleOauth2)GetLogoutUrl(w http.ResponseWriter, r *http.Request) string {
	loginUrl := makeUrlAbsolute(r, "/")

	args1 := url.Values{}
	args1.Add("continue", loginUrl)

	args2 := url.Values{}
	args2.Add("continue", "https://appengine.google.com/_ah/logout?" + args1.Encode())
	
	// This weird thing bounces you through a "are you sure you want to redirect" notice,
	// which is fine. You can't just redirect anywhere, it has to go to a google url.
	return "https://www.google.com/accounts/Logout?" + args2.Encode()
}

// }}}
// {{{ goauth2.CallbackToEmail

func (goauth2 GoogleOauth2)CallbackToEmail(r *http.Request) (string, error) {
	// Read oauthState from Cookie
	if oauthState,err := r.Cookie("oauthstate"); err != nil {
		log.Printf("no oauth google cookie: %v", err)
		return "", fmt.Errorf("no oauth google cookie: %v", err)
	} else if r.FormValue("state") != oauthState.Value {
		return "", fmt.Errorf("invalid oauth google state")
	}

	email,err := goauth2.getEmailFromGoogle(r.FormValue("code"))
	return email,err
}

{'fraction_non_alphanumeric': 0.13961218836565098, 'fraction_numerical': 0.012927054478301015, 'mean_word_length': 4.114258734655335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 6, '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': '7008884', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1883, 'n_words': 442}