コード例 #1
0
ファイル: oauth.go プロジェクト: sguzwf/vertex
func (o *OAuthMiddleware) LoginHandler() vertex.Route {

	handler := func(w http.ResponseWriter, r *vertex.Request) (interface{}, error) {
		code := r.FormValue("code")
		logging.Info("Got code: %s", code)

		tok, err := o.conf.Exchange(oauth2.NoContext, code)
		if err != nil {
			return nil, vertex.UnauthorizedError("Could not log you in: %s", err)
		}

		user, err := o.userValidator.Login(tok)
		if err != nil {
			return nil, vertex.UnauthorizedError("Could not validate user for login: %s", err)
		}

		enc, err := o.userValidator.EncodeToken(user)
		if err != nil {
			return nil, vertex.UnauthorizedError("Could not validate encode user token: %s", err)
		}

		o.setCookie(w, enc, r.Host)

		if cook, err := r.Cookie(nextUrl); err == nil && cook != nil && cook.Value != "" {
			logging.Info("Found nextUrl from before auth denied. Redirecting to %s", cook.Value)
			http.Redirect(w, r.Request, cook.Value, http.StatusTemporaryRedirect)
			return nil, vertex.Hijacked
		}

		return "Success Logging In", nil
	}
	return vertex.Route{
		Path:        loginPath,
		Description: "OAuth Login",
		Handler:     vertex.HandlerFunc(handler),
		Methods:     vertex.GET,
	}

}
コード例 #2
0
ファイル: middleware_test.go プロジェクト: sguzwf/vertex
package middleware

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/EverythingMe/vertex"
)

var mockkHandler = vertex.HandlerFunc(func(w http.ResponseWriter, r *vertex.Request) (interface{}, error) {
	return nil, nil
})

func TestIPFilter(t *testing.T) {

	flt := NewIPRangeFilter().AllowPrivate()
	flt.Allow("8.8.8.4")
	flt.Deny("127.0.0.2")
	hr, _ := http.NewRequest("GET", "/foo", nil)
	r := vertex.NewRequest(hr)
	checkAddr := func(addr string) error {
		r.RemoteIP = addr
		_, err := flt.Handle(httptest.NewRecorder(), r, mockkHandler)
		return err
	}

	assert.NoError(t, checkAddr("127.0.0.1"))
	assert.NoError(t, checkAddr("172.16.25.46"))
コード例 #3
0
ファイル: swagger_test.go プロジェクト: sguzwf/vertex
func TestSwagger(t *testing.T) {
	//t.SkipNow()
	a := &vertex.API{
		Name:          "testung",
		Version:       "1.0",
		Doc:           "Our fancy testung API",
		Title:         "Testung API!",
		Middleware:    middleware.DefaultMiddleware,
		Renderer:      vertex.JSONRenderer{},
		AllowInsecure: true,
		Routes: vertex.Routes{
			{
				Path:        "/user/{id}",
				Description: "Get User Info by id or name",
				Handler:     UserHandler{},
				Methods:     vertex.GET,
			},
			{
				Path:        "/func/handler",
				Description: "Test handling by a pure func",
				Methods:     vertex.POST,
				Handler: vertex.HandlerFunc(func(w http.ResponseWriter, r *vertex.Request) (interface{}, error) {
					return "WAT WAT", nil
				}),
			},
		},
	}

	srv := vertex.NewServer(":9947")
	srv.AddAPI(a)

	s := httptest.NewServer(srv.Handler())
	defer s.Close()

	u := fmt.Sprintf("http://%s%s", s.Listener.Addr().String(), a.FullPath("/swagger"))
	t.Log(u)

	res, err := http.Get(u)
	if err != nil {
		t.Errorf("Could not get swagger data")
	}

	defer res.Body.Close()
	//	b, err := ioutil.ReadAll(res.Body)
	//	fmt.Println(string(b))
	var sw swagger.API
	dec := json.NewDecoder(res.Body)
	if err = dec.Decode(&sw); err != nil {
		t.Errorf("Could not decode swagger def: %s", err)
	}

	swexp := a.ToSwagger(s.Listener.Addr().String())

	assertEqual(t, sw.Basepath, swexp.Basepath)
	assertEqual(t, sw.Consumes, swexp.Consumes)
	assertEqual(t, sw.Host, swexp.Host)
	assertEqual(t, sw.Info, swexp.Info)
	assertEqual(t, sw.Produces, swexp.Produces)
	assertEqual(t, sw.Schemes, swexp.Schemes)
	assertEqual(t, sw.SwaggerVersion, swexp.SwaggerVersion)

	for k, v := range swexp.Paths {

		v2 := sw.Paths[k]
		assertEqual(t, v, v2, "Path mismatch \n%#v\n%#v")
	}

	//fmt.Println(sw)

}