Пример #1
0
func withApp(context martini.Context, params martini.Params, res http.ResponseWriter) {
	app := appreg.Get(params["name"])
	if app == nil {
		http.Error(res, "No such application.", 404)
	}
	context.Map(app)
}
Пример #2
0
// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, context martini.Context, errors *Errors, ifacePtr ...interface{}) {
	context.Invoke(Validate(obj.Interface()))
	errors.combine(getErrors(context))
	context.Map(*errors)
	context.Map(obj.Elem().Interface())
	if len(ifacePtr) > 0 {
		context.MapTo(obj.Elem().Interface(), ifacePtr[0])
	}
}
Пример #3
0
func create_identity(c martini.Context, tx *sqlx.Tx) {
	identity := &data.Identity{}
	err := data.CreateIdentity(tx, identity)
	if err != nil {
		panic(err)
	}

	c.Map(identity)
}
Пример #4
0
func Middleware(ctx martini.Context, r *http.Request, w http.ResponseWriter) {
	sessionId := ensureCookie(r, w)
	session := sessionStore.Get(sessionId)

	ctx.Map(session)

	ctx.Next()

	sessionStore.Set(session)
}
Пример #5
0
func RequireLogin(rw http.ResponseWriter, req *http.Request,
	s sessions.Session, db *sql.DB, c martini.Context) {
	user := &User{}
	err := db.QueryRow("select name, email from users where id=$1", s.Get("userId")).Scan(&user.Name, &user.Email)

	if err != nil {
		http.Redirect(rw, req, "/login", http.StatusFound)
		return
	}

	c.Map(user)
}
Пример #6
0
func ConnectDB(r *http.Request, c martini.Context) string {

	host := r.FormValue("host")
	port := r.FormValue("port")
	client := riakpbc.NewClient([]string{
		host + ":" + port,
	})

	c.Map(client)
	return "connected"

}
Пример #7
0
func may_authenticate(c martini.Context, sess sessions.Session, db *sqlx.DB, r *http.Request) {
	var (
		interactive = true
		token       string
		identity_id int64
		identity    *data.Identity
		err         error
	)

	// Attempt with Authorization header
	if v := r.Header.Get("Authorization"); v != "" {
		parts := strings.SplitN(v, " ", 2)
		if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
			interactive = false
			token = parts[1]
		}

		// Attempt with access_token parameter
	} else if v := r.URL.Query().Get("access_token"); v != "" {
		interactive = false
		token = v

		// Attempt with session.identity_id
	} else if id, ok := sess.Get("identity_id").(int64); ok {
		interactive = true
		identity_id = id
	}

	if token != "" {
		at, err := data.GetAccessTokenWithAccessToken(db, token)
		if err != nil {
			panic(err)
		}
		identity_id = at.IdentityId
	}

	if identity_id > 0 {
		identity, err = data.GetIdentity(db, identity_id)
		if err != nil {
			panic(err)
		}
	}

	if interactive {
		r.Header.Set("x-interactive", "true")
	}

	c.Map(identity)
}
Пример #8
0
func wsHandshake(context martini.Context, w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}
	ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		http.Error(w, fmt.Sprintf("error: %s", err), 500)
		log.Println(err)
		return
	}
	context.Map(ws)
}
Пример #9
0
// MapEncoder intercepts the request's URL, detects the requested format,
// and injects the correct encoder dependency for this request. It rewrites
// the URL to remove the format extension, so that routes can be defined
// without it.
func MapEncoder(c martini.Context, w http.ResponseWriter, r *http.Request) {
	// Get the format extension
	matches := rxExt.FindStringSubmatch(r.URL.Path)
	ft := ".json"
	if len(matches) > 1 {
		// Rewrite the URL without the format extension
		l := len(r.URL.Path) - len(matches[1])
		if strings.HasSuffix(r.URL.Path, "/") {
			l--
		}
		r.URL.Path = r.URL.Path[:l]
		ft = matches[1]
	}
	// Inject the requested encoder
	switch ft {
	case ".xml":
		c.Map(xmlEncoder)
		w.Header().Set("Content-Type", "application/xml")
	case ".text":
		c.Map(textEncoder)
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	default:
		c.Map(jsonEncoder)
		w.Header().Set("Content-Type", "application/json")
	}
}
Пример #10
0
// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, context martini.Context, errors *Errors) {
	context.Invoke(Validate(obj.Interface()))
	errors.combine(getErrors(context))
	context.Map(*errors)
	context.Map(obj.Elem().Interface())
}
Пример #11
0
func appEngine(c martini.Context, r *http.Request) {
	c.Map(appengine.NewContext(r))
}
Пример #12
0
func PopulateAppContext(martiniContext martini.Context, w http.ResponseWriter, request *http.Request, renderer render.Render) {
	dbContext := models.NewDbContext()
	appContext := &models.AppContext{Request: request, Renderer: renderer, MartiniContext: martiniContext, DbContext: dbContext}

	martiniContext.Map(appContext)
}
Пример #13
0
func GET_callback(c martini.Context, sess sessions.Session, r *http.Request, db *sqlx.DB) {
	flow, ok := sess.Get("flow").(FlowState)
	if !ok {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.StartAt.Before(time.Now().Add(-10 * time.Minute)) {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.State == "" {
		c.Invoke(redirect_to("/login"))
		return
	}
	if r.URL.Query().Get("code") == "" {
		c.Invoke(redirect_to("/login"))
		return
	}
	if flow.State != r.URL.Query().Get("state") {
		c.Invoke(redirect_to("/login"))
		return
	}

	var (
		provider  = tmp_new_provider(flow.Provider)
		transport = provider.Transport(nil)
	)

	token, err := transport.Exchange(r.URL.Query().Get("code"))
	if err != nil {
		panic(err)
	}

	profile, err := provider.GetProfile(transport)
	if err != nil {
		panic(err)
	}

	var (
		tx      = db.MustBegin()
		success bool
	)

	defer func() {
		if success {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()

	account, err := data.GetAccountWithRemoteId(tx, profile.RemoteId())
	if err != nil {
		panic(err)
	}

	c.MapTo(profile, (*providers.Profile)(nil))
	c.Map(token)
	c.Map(tx)
	c.Map(account)

	if account != nil {
		c.Invoke(GET_callback_A)
	} else {
		c.Invoke(GET_callback_B)
	}

	success = true
}