func (a *App) RunMethod(name string, r kit.Request, responder func(kit.Response), withFinishedChannel bool) (chan bool, apperror.Error) { method := a.registry.Method(name) if method == nil { return nil, &apperror.Err{ Code: "unknown_method", Message: fmt.Sprintf("The method %v does not exist", name), } } if r.GetSession() == nil { session, err := a.UserService().StartSession(r.GetUser(), "") if err != nil { return nil, err } r.SetSession(session) } instance := NewMethodInstance(method, r, responder) if withFinishedChannel { c := make(chan bool) instance.finishedChannel = c return c, a.sessionManager.QueueMethod(r.GetSession(), instance) } else { return nil, a.sessionManager.QueueMethod(r.GetSession(), instance) } }
func AuthenticationMiddleware(registry kit.Registry, r kit.Request) (kit.Response, bool) { // Handle authentication. httpRequest := r.GetHttpRequest() userService := registry.UserService() if userService == nil { return nil, false } authHeader := httpRequest.Header.Get("Authentication") if authHeader == "" { return nil, false } // Check for basic auth. if strings.HasPrefix(authHeader, "Basic ") { str := authHeader[6:] data, err := base64.StdEncoding.DecodeString(str) if err != nil { return kit.NewErrorResponse("invalid_basic_auth"), false } else { parts := strings.Split(string(data), ":") if len(parts) == 2 { userIdentifier := parts[0] pw := parts[1] user, err := userService.AuthenticateUser(userIdentifier, "password", map[string]interface{}{"password": pw}) if err != nil { return kit.NewErrorResponse(err), false } r.SetUser(user) return nil, false } } } // Check for auth token. if authHeader != "" { token := authHeader user, session, err := userService.VerifySession(token) if err == nil { r.SetUser(user) r.SetSession(session) return nil, false } else { return kit.NewErrorResponse(err), false } } return nil, false }