Exemplo n.º 1
0
// RequestResource returns the resource targeted by the CORS request defined in ctx.
func (v Specification) RequestResource(ctx *goa.Context, origin string) *ResourceDefinition {
	path := ctx.Request().URL.Path
	var match *ResourceDefinition
	for _, res := range v {
		if res.OriginAllowed(origin) && res.PathMatches(path) {
			if res.Check == nil || res.Check(ctx) {
				match = res
				break
			}
		}
	}
	return match
}
Exemplo n.º 2
0
func parseBasicAuth(ctx *goa.Context) (username, password string, ok bool) {
	auth := ctx.Request().Header.Get("Authorization")
	if auth == "" {
		return
	}

	if !strings.HasPrefix(auth, basicScheme) {
		return
	}

	c, err := base64.StdEncoding.DecodeString(auth[len(basicScheme):])
	if err != nil {
		return
	}

	cs := string(c)
	s := strings.IndexByte(cs, ':')
	if s < 0 {
		return
	}

	return cs[:s], cs[s+1:], true
}
Exemplo n.º 3
0
// GetToken extracts the JWT token from the request if there is one.
func GetToken(ctx *goa.Context, spec *Specification) (token *jwt.Token, err error) {
	var found bool
	var tok string
	header := ctx.Request().Header.Get(spec.TokenHeader)

	if header != "" {
		parts := strings.Split(header, " ")
		if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
			err = fmt.Errorf("Malformed token header")
			return
		}
		tok = parts[1]
		found = true
	}
	if !found && spec.AllowParam {
		tok = ctx.Request().URL.Query().Get(spec.TokenParam)
	}
	if tok == "" {
		err = fmt.Errorf("no token")
		return
	}
	token, err = jwt.Parse(tok, keyFuncWrapper(spec.ValidationFunc))
	return
}
Exemplo n.º 4
0
		})

		It("sets the response writer and returns the previous one", func() {
			rwo := ctx.SetResponseWriter(rw)
			Ω(rwo).Should(BeNil())
			rwo = ctx.SetResponseWriter(&TestResponseWriter{Status: 43})
			Ω(rwo).ShouldNot(BeNil())
			Ω(rwo).Should(BeAssignableToTypeOf(&TestResponseWriter{}))
			trw := rwo.(*TestResponseWriter)
			Ω(trw.Status).Should(Equal(42))
		})
	})

	Describe("Request", func() {
		It("returns nil if not initialized", func() {
			Ω(ctx.Request()).Should(BeNil())
		})
	})

	Describe("Header", func() {
		It("returns nil if not initialized", func() {
			Ω(ctx.Header()).Should(BeNil())
		})
	})

	Describe("ResponseStatus", func() {
		It("returns 0 if not initialized", func() {
			Ω(ctx.ResponseStatus()).Should(Equal(0))
		})
	})