func (gm *GMServer) authnMiddleware(inner http.Handler) http.Handler {
	mw := func(w http.ResponseWriter, r *http.Request) {
		// TODO: use https://github.com/abbot/go-http-auth for digest auth
		token, ok := parseBearerAuth(r)

		if !ok {
			// When connecting via websocket protocol, JavaScript API does not have a
			// way to provide HTTP authorization header, so we have to use a trick
			// here: get token from the query string.
			token = r.FormValue("token")
			if token != "" {
				glog.V(2).Infof("Getting token from the query string")
				ok = true
			}
		}

		if ok {
			var ud *storage.UserData
			err := gm.si.Tx(func(tx *sql.Tx) error {
				ud2, err := gm.si.GetUserByAccessToken(tx, token)

				if err != nil {
					return errors.Trace(err)
				}

				ud = ud2
				return nil
			})
			if err != nil {
				w.Header().Set("WWW-Authenticate", "Bearer realm=\"login please\"")
				hh.RespondWithError(w, r, err)
				return
			}

			// Authn data is correct: create a new request with updated context
			ctx := r.Context()
			ctx = context.WithValue(ctx, "authUserData", ud)
			r = r.WithContext(ctx)
		}

		// Process request, whether authn data was not provided at all, or was
		// provided correctly.
		inner.ServeHTTP(w, r)
	}
	return middleware.MkMiddleware(mw)
}
// Middleware which populates the context with the authentication data, if
// it is provided and is correct.
//
// If it's provided but isn't correct, responds with an error. TODO: do we
// really need that behaviour? Maybe it's better to just proceed without authn
// data? Dunno.
//
// NOTE: be sure to use it after httphelper.MakeDesiredContentTypeMiddleware(),
// since the error response should be in the right format
Middleware which populates the context with the authentication data, if
it is provided and is correct.

If it's provided but isn't correct, responds with an error. TODO: do we
really need that behaviour. Maybe it's better to just proceed without authn
data. Dunno.

be sure to use it after httphelper.MakeDesiredContentTypeMiddleware(),
since the error response should be in the right format
// Middleware which populates the context with the authentication data if // it is provided and is correct . // // If it ' s provided but isn ' t correct responds with an error . TODO : do we // really need that behaviour? Maybe it ' s better to just proceed without authn // data? Dunno . // // NOTE : be sure to use it after httphelper . MakeDesiredContentTypeMiddleware () // since the error response should be in the right format
func (gm *GMServer) authnMiddleware(inner http.Handler) http.Handler {
	mw := func(w http.ResponseWriter, r *http.Request) {
		token, ok := parseBearerAuth(r)
		if !ok {
			token = r.FormValue("token")
			if token != "" {
				glog.V(2).Infof("Getting token from the query string")
				ok = true
			}
		}
		if ok {
			var ud *storage.UserData
			err := gm.si.Tx(func(tx *sql.Tx) error {
				ud2, err := gm.si.GetUserByAccessToken(tx, token)
				if err != nil {
					return errors.Trace(err)
				}
				ud = ud2
				return nil
			})
			if err != nil {
				w.Header().Set("WWW-Authenticate", "Bearer realm=\"login please\"")
				hh.RespondWithError(w, r, err)
				return
			}
			ctx := r.Context()
			ctx = context.WithValue(ctx, "authUserData", ud)
			r = r.WithContext(ctx)
		}
		inner.ServeHTTP(w, r)
	}
	return middleware.MkMiddleware(mw)
}
func ( gm * GMServer ) authnMiddleware ( inner http . Handler ) http . Handler { mw := func ( w http . ResponseWriter , r * http . Request ) { token , ok := parseBearerAuth ( r ) if ! ok { token = r . FormValue ( " " ) if token != " " { glog . V ( 2 ) . Infof ( " " ) ok = true } } if ok { var ud * storage . UserData err := gm . si . Tx ( func ( tx * sql . Tx ) error { ud2 , err := gm . si . GetUserByAccessToken ( tx , token ) if err != nil { return errors . Trace ( err ) } ud = ud2 return nil } ) if err != nil { w . Header ( ) . Set ( " " , " \" \" " ) hh . RespondWithError ( w , r , err ) return } ctx := r . Context ( ) ctx = context . WithValue ( ctx , " " , ud ) r = r . WithContext ( ctx ) } inner . ServeHTTP ( w , r ) } return middleware . MkMiddleware ( mw ) }
Middleware which populates the context with the authentication data, if
it is provided and is correct.
Middleware which populates the context with the authentication data if it is provided and is correct .
// TODO: use https://github.com/abbot/go-http-auth for digest auth // When connecting via websocket protocol, JavaScript API does not have a // way to provide HTTP authorization header, so we have to use a trick // here: get token from the query string. // Authn data is correct: create a new request with updated context // Process request, whether authn data was not provided at all, or was // provided correctly.