示例#1
0
文件: endpoints.go 项目: catanm/gms
func Connect(ctx context.Context) error {
	provider_type := ctx.PathValue("provider")
	action := ctx.PathValue("action")

	if provider_type == "facebook" {
		provider, err := gomniauth.Provider(provider_type)
		if err != nil {
			log.Error("Error on getting provider: " + err.Error())
			return goweb.API.Respond(ctx, 200, nil, []string{"An error has occured."})
		}
		state := gomniauth.NewState("after", "success")
		// if you want to request additional scopes from the provider,
		// pass them as login?scope=scope1,scope2
		//options := objx.MSI("scope", ctx.QueryValue("scope"))
		authUrl, err := provider.GetBeginAuthURL(state, nil)
		if err != nil {
			log.Error("Error on getting url: " + err.Error())
			return goweb.API.Respond(ctx, 200, nil, []string{"An error has occured."})
		}
		// redirect
		return goweb.Respond.WithRedirect(ctx, authUrl)
	} else if provider_type == "local" && ctx.MethodString() == "POST" {
		// This is taken care of in separate functions.
		// Local login only with POST
		if action == "login" {
			return nil
		} else if action == "register" {
			return nil
		} else if action == "connect" {
			return nil
		} else {
			return goweb.API.Respond(ctx, 200, nil, []string{"Invalid action."})
		}
	} else {
		return goweb.API.Respond(ctx, 200, nil, []string{"Invalid provider type."})
	}
}
示例#2
0
// WillHandle checks whether this handler will be used to handle the specified
// request or not.
func (p *PathMatchHandler) WillHandle(c context.Context) (bool, error) {

	// check each matcher func
	matcherFuncMatches := true
	matcherFuncDecisionMade := false
	for _, matcherFunc := range p.MatcherFuncs {
		decision, matcherFuncErr := matcherFunc(c)

		if matcherFuncErr != nil {
			return false, matcherFuncErr
		}

		switch decision {
		case NoMatch:
			matcherFuncMatches = false
			matcherFuncDecisionMade = true
			break
		case Match:
			matcherFuncMatches = true
			matcherFuncDecisionMade = true
			break
		}

		if matcherFuncDecisionMade {
			break
		}

	}

	// check HTTP methods
	var httpMethodMatch bool = false

	if len(p.HttpMethods) == 0 {

		// no specific HTTP methods
		httpMethodMatch = true

	} else {

		for _, httpMethod := range p.HttpMethods {
			if httpMethod == c.MethodString() {
				httpMethodMatch = true
				break
			}
		}

	}

	// cancel early if we didn't get an HTTP Method match
	if !httpMethodMatch {
		return false, nil
	}

	// check path match

	pathMatch := p.PathPattern.GetPathMatch(c.Path())

	var allMatch bool

	if matcherFuncDecisionMade {
		allMatch = matcherFuncMatches
	} else {
		allMatch = pathMatch.Matches
	}

	if allMatch {

		// save the match parameters for later
		c.Data().Set(context.DataKeyPathParameters, pathMatch.Parameters)

	}

	return allMatch, nil
}