Esempio n. 1
0
//simulate a demo login, create the cookie, make sure the demo user exists, create the session
func DemoUser(w http.ResponseWriter, r *http.Request) {
	demo_email := "*****@*****.**"
	var us User
	var err error
	if !UserExists(demo_email) {
		us, err = AddUser(demo_email)
		if err != nil {
			glog.Errorf("DemoUser(w,r)AddUser(%s): %s", demo_email, err)
			return
		}
	} else {
		us, err = GetUserByEmail(demo_email)
		if err != nil {
			glog.Errorf("DemoUser(w,r)GetUserByEmail(%s): %s", demo_email, err)
			return
		}
	}
	var authString = u.RandomString(64)
	//set the cookie
	err = us.AddSession(authString)
	if err != nil {
		glog.Errorf("DemoUser(w,r)AddUser(%s): %s", authString, err)
		return
	}
	expire := time.Now().AddDate(1, 0, 0) // year expirey seems reasonable
	cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire}
	http.SetCookie(w, &cookie)
	http.Redirect(w, r, "/main", http.StatusFound)
}
Esempio n. 2
0
func AddSession(w http.ResponseWriter, r *http.Request, email string) (err error) {
	var us User
	if !UserExists(email) {
		fmt.Printf("HandleOauth2Callback: creating new user %s", email)
		us, err = AddUser(email)
		if err != nil {
			glog.Errorf("HandleOauth2Callback:UserExists()AddUser(%s): %s", email, err)
			return err
		}
	} else {
		us, err = GetUserByEmail(email)
		if err != nil {
			glog.Errorf("HandleOauth2Callback:UserExists()GetUserEmail(%s): %s", email, err)
			return err
		}
	}
	var authString = u.RandomString(64)

	err = us.AddSession(authString)

	if err != nil {
		glog.Errorf("HandleOauth2Callback:stmtCookieIns.Exec(%s,%s): %s", us.ID, authString, err)
		return err
	}
	//set the cookie
	expire := time.Now().AddDate(1, 0, 0) // year expirey seems reasonable
	cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire}
	http.SetCookie(w, &cookie)
	http.Redirect(w, r, "/main", http.StatusFound)
	return err
}
Esempio n. 3
0
// Function that handles the callback from the Google server
func HandleOAuth2Callback(w http.ResponseWriter, r *http.Request) {
	//Get the code from the response
	code := r.FormValue("code")

	t := &oauth.Transport{Config: oauthCfg}

	// Exchange the received code for a token
	_, err := oauthCfg.TokenCache.Token()
	if err != nil {
		_, err := t.Exchange(code)
		if err != nil {
			glog.Errorf("HandleOauth2Callback:oauthCfg.TokenCache.Token():t.Exchange(%s): %s", code, err)
		}
	}

	// Make the request.
	req, err := t.Client().Get(profileInfoURL)
	if err != nil {
		glog.Errorf("HandleOauth2Callback:t.Client().Get(%s): %s", profileInfoURL, err)
		return
	}
	defer req.Body.Close()
	body, _ := ioutil.ReadAll(req.Body)
	//body.id is the google id to use
	//set a cookie with the id, and random hash. then save the id/hash pair to db for lookup
	var f interface{}
	err = json.Unmarshal(body, &f)
	if err != nil {
		glog.Errorf("HandleOauth2Callback:json.Unmarshal(%s): %s", body, err)
		return
	}
	m := f.(map[string]interface{})
	var authString = u.RandomString(64)
	email := m["email"].(string)
	var us User
	if !UserExists(email) {
		glog.Infof("HandleOauth2Callback: creating new user %s", email)
		us, err = AddUser(email)
		if err != nil {
			glog.Errorf("HandleOauth2Callback:UserExists()AddUser(%s): %s", email, err)
		}
	} else {
		us, err = GetUserByEmail(email)
		if err != nil {
			glog.Errorf("HandleOauth2Callback:UserExists()GetUserEmail(%s): %s", email, err)
		}
	}

	err = us.AddSession(authString)

	if err != nil {
		glog.Errorf("HandleOauth2Callback:stmtCookieIns.Exec(%s,%s): %s", us.ID, authString, err)
	}
	//set the cookie
	expire := time.Now().AddDate(1, 0, 0) // year expirey seems reasonable
	cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire}
	http.SetCookie(w, &cookie)
	http.Redirect(w, r, "/main", http.StatusFound)
}
Esempio n. 4
0
func (us User) NewLoginCode() string {
	newstr := u.RandomString(128)
	_, err := stmtInsertUserLogin.Exec(newstr, us.ID)
	if err != nil {
		glog.Errorf("stmtInsertUserLogin(%s,%s): %s", newstr, us.ID, err)
		return ""
	}
	return us.LoginCode()
}
Esempio n. 5
0
func (us User) NewShareCode() string {
	newstr := u.RandomString(128)
	_, err := stmtInsertUserShare.Exec(newstr, us.ID)
	if err != nil {
		glog.Errorf("stmtInsertUserShare.Exec(%s,%s): %s", us.ID, newstr, err)
		return ""
	}
	return us.ShareCode()
}
Esempio n. 6
0
func (us User) LoginCode() (lc string) {
	err := stmtGetUserLogin.QueryRow(us.ID).Scan(&lc)
	switch {
	case err == sql.ErrNoRows || lc == "":
		glog.Infof("No existing login code")
		newstr := u.RandomString(128)
		_, err := stmtInsertUserLogin.Exec(newstr, us.ID)
		if err != nil {
			glog.Errorf("stmtInsertUserLogin(%s,%s): %s", newstr, us.ID, err)
			return ""
		}
		return newstr
	case err != nil:
		glog.Errorf("stmtGetLoginShare.QueryRow(%s): %s", us.ID, err)
		return ""
	}
	return lc
}
Esempio n. 7
0
func (us User) ShareCode() string {
	var sc string
	err := stmtGetUserShare.QueryRow(us.ID).Scan(&sc)
	switch {
	case err == sql.ErrNoRows || sc == "":
		glog.Infof("No existing share code")
		newstr := u.RandomString(128)
		_, err := stmtInsertUserShare.Exec(newstr, us.ID)
		if err != nil {
			glog.Errorf("stmtInsertUserShare.Exec(%s,%s): %s", us.ID, newstr, err)
			return ""
		}
		return newstr
	case err != nil:
		glog.Errorf("stmtGetUserShare.QueryRow(%s): %s", us.ID, err)
		return ""
	}
	return sc
}
Esempio n. 8
0
func LoginToken(w http.ResponseWriter, r *http.Request, lt string) (err error) {
	us, err := GetUserByLoginToken(lt)
	fmt.Printf("got user: %s", us)
	if err != nil {
		glog.Errorf("LoginToken(%s) No session by that token: %s", lt, err)
		return err
	}
	var authString = u.RandomString(64)
	fmt.Printf("AddSession(%s)", authString)
	err = us.AddSession(authString)
	if err != nil {
		glog.Errorf("LoginToken():us.AddSession(%s): %s", authString, err)
		return err
	}
	expire := time.Now().AddDate(1, 0, 0)
	cookie := http.Cookie{Name: cookieName, Value: authString, Expires: expire, Path: "/"}
	fmt.Printf("http.SetCookie(w,%s)expore:%s", cookie, expire)
	http.SetCookie(w, &cookie)
	http.Redirect(w, r, "/main.html", http.StatusFound)
	return err
}
Esempio n. 9
0
	stmtSessionExists *sql.Stmt
	googleEnabled     bool
	facebookEnabled   bool
	GoogOauthCfg      = &oauth2.Config{
		ClientID:     "",
		ClientSecret: "",
		Scopes:       []string{"https://www.googleapis.com/auth/userinfo.email"},
		Endpoint:     googleoauth.Endpoint,
	}
	FBOauthCfg = &oauth2.Config{
		ClientID:     "",
		ClientSecret: "",
		Scopes:       []string{"email"},
		Endpoint:     fboauth.Endpoint,
	}
	oauthStateString = u.RandomString(32)
)

func CookieName(c string) {
	cookieName = c
}
func Environment(e string) {
	environment = e
}
func DB(d *sql.DB) {
	db = d
	var err error
	userDB()
	stmtCookieIns, err = u.Sth(db, "INSERT INTO sessions (user_id,session_hash) VALUES( ? ,?  )")
	if err != nil {
		glog.Fatalf(" DB(): u.sth(stmtCookieIns) %s", err)