Example #1
0
// AuthenticateHandler returns a web handler function that redirects to a
// session-specific authentication link.
func AuthenticateHandler(auth evesso.Authenticator, sess server.Sessionizer) web.HandlerFunc {
	return func(c web.C, w http.ResponseWriter, r *http.Request) {
		s := sess.GetSession(&c, w, r)
		url := auth.URL(s.State)
		http.Redirect(w, r, url, http.StatusFound)
	}
}
Example #2
0
// SessionInfo returns a web handler function that returns information about the
// current session.
func SessionInfo(auth evesso.Authenticator, sess server.Sessionizer, localdb db.LocalDB) web.HandlerFunc {
	return func(c web.C, w http.ResponseWriter, r *http.Request) {
		curSession := sess.GetSession(&c, w, r)
		returnInfo := sessionInfo{
			Authenticated: curSession.User != 0,
			OAuthURL:      auth.URL(curSession.State),
		}
		if curSession.Token != nil {
			returnInfo.OAuthExpiry = curSession.Token.Expiry
		}
		if curSession.User != 0 {
			// We're authenticated - also pass in the API keys registered to this
			// user.
			keys, err := localdb.APIKeys(curSession.User)
			if err != nil {
				log.Fatalf("Error - unable to retrieve API keys from database.")
			}
			returnInfo.APIKeys = keys
		}
		returnJSON, _ := json.Marshal(&returnInfo)
		w.Write(returnJSON)
	}
}