Esempio n. 1
0
File: login.go Progetto: knq/oauthmw
// sessionStore returns the oauthmw session store.
func (l login) sessionStore(ctxt context.Context) *Store {
	// get store from session
	obj, ok := sessionmw.Get(ctxt, l.provider.SessionKey)
	if ok {
		store, ok := obj.(Store)
		if !ok {
			// this shouldn't ever happen ...
			log.Println("CORRUPTED/MALFORMED SESSION STORAGE. OVERWRITING")
			store = Store{
				Provider: "",
				Token:    &oauth2.Token{},
				States:   make(map[string]StoreState),
			}

			sessionmw.Set(ctxt, l.provider.SessionKey, store)
			return &store
		}

		return &store
	}

	// create new store in session and return
	store := Store{
		Provider: "",
		Token:    &oauth2.Token{},
		States:   make(map[string]StoreState),
	}

	sessionmw.Set(ctxt, l.provider.SessionKey, store)
	return &store
}
Esempio n. 2
0
func UserLoginHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	if !cas.IsAuthenticated(r) {
		// save the referrer
		sessionmw.Set(ctx, CASReferrer, r.Referer())

		// shut off rendering
		dataRenderer := data.FromContext(ctx)
		dataRenderer.Type = data.DataNoRender

		// and redirect
		cas.RedirectToLogin(rw, r)
	} else {
		// get the referrer
		referrer, has := sessionmw.Get(ctx, CASReferrer)
		sessionmw.Delete(ctx, CASReferrer)

		// shut off rendering
		dataRenderer := data.FromContext(ctx)
		dataRenderer.Type = data.DataNoRender

		// and redirect
		if !has {
			http.Redirect(rw, r, render.ConvertURL("/"), http.StatusTemporaryRedirect)
		} else {
			http.Redirect(rw, r, referrer.(string), http.StatusTemporaryRedirect)
		}
	}
}