Esempio n. 1
0
// Handler to require a user to log in. If the user is currently logged in
// nothing happens. Otherwise clear existing session and redirect the user
// to the login page
func RequireLogin(s sessions.Session, r render.Render) {
	session := s.Get("user_session")
	if session == nil {
		s.Clear()
		r.Redirect("/login")
	}
}
Esempio n. 2
0
func DoLogin(entity models.UserLogin, session sessions.Session, enc Encoder, db gorp.SqlExecutor) (int, string) {

	status := &models.DefaultStruct{
		Id:       http.StatusForbidden,
		Name:     messages.GetLocaleMessage("en", "MSG_LOGIN_INVALID"),
		Lang_key: "en",
	}

	user := GetUserByLogin(entity, db)

	if user.Name != "" {

		err := sessionauth.AuthenticateSession(session, user)
		if err != nil {
			status.Name = messages.GetLocaleMessage("en", "MSG_SESSION_INIT_ERROR")
			return http.StatusForbidden, Must(enc.EncodeOne(status))
		}
		user.Authenticated = true
		user.UpdateLastLogin()
		status.Name = messages.GetLocaleMessage("en", "MSG_SUCCESSFUL_LOGIN")
		return http.StatusOK, Must(enc.EncodeOne(user))

	} else {

		sessionauth.Logout(session, user)
		session.Clear()
		return http.StatusForbidden, Must(enc.EncodeOne(status))

	}

	return http.StatusForbidden, Must(enc.EncodeOne(status))
}
Esempio n. 3
0
/* Middleware to:
   - Retrieve data from github if user is logged in and data is not stored in sessions
   - Delete session data if user logged out */
func RequestData(tokens oauth2.Tokens, session sessions.Session) {
	if !tokens.Expired() && session.Get("username") == nil {
		access := tokens.Access()
		req, _ := http.NewRequest("GET", "https://api.github.com/user?access_token="+access, nil)
		client := &http.Client{}
		resp, _ := client.Do(req)

		body, _ := ioutil.ReadAll(resp.Body)

		parse := map[string]interface{}{}
		json.Unmarshal([]byte(string(body)), &parse)
		session.Set("username", parse["login"])
		session.Set("user_id", parse["id"])
		session.Set("avatar", parse["avatar_url"])

		log.Println("retrieved data")
	} else if tokens.Expired() && session.Get("username") != nil {
		session.Clear()
		log.Println("removed data")
	}
}
Esempio n. 4
0
func DoLogout(enc Encoder, session sessions.Session, user sessionauth.User, db gorp.SqlExecutor) (int, string) {

	status := &models.DefaultStruct{
		Id:       http.StatusOK,
		Name:     messages.GetLocaleMessage("en", "MSG_LOGIN_REQUIRED"),
		Lang_key: "en",
	}

	if user.IsAuthenticated() {

		sessionauth.Logout(session, user)
		session.Clear()
		status.Name = messages.GetLocaleMessage("en", "MSG_SUCCESSFUL_LOGOUT")
	}

	updateUser := UserById(user.UniqueId().(int), db)

	updateUser.Authenticated = false
	db.Update(updateUser)

	return http.StatusOK, Must(enc.EncodeOne(status))
}
Esempio n. 5
0
func (web *MailWeb) welcome(session sessions.Session, r render.Render) {
	session.Clear()
	r.HTML(200, "start", nil)
}
Esempio n. 6
0
func LogoutAction(session sessions.Session, re render.Render) {
	session.Clear()
	re.Redirect("/")
}
Esempio n. 7
0
// GET /logout
func GetLogout(s sessions.Session, r render.Render) {
	s.Clear()
	r.Redirect("/login")
}
Esempio n. 8
0
func LogOut(session sessions.Session) string {
	session.Clear()
	return "退出成功"
}