// CreateAuthMiddleware creates the middleware for authtication
func CreateAuthMiddleware() (*jwt.Middleware, error) {
	err := variables.LoadTokenKeys()
	if err != nil {
		return nil, err
	}

	authMiddleware := &jwt.Middleware{
		Realm:            "hangouts-call-center",
		SigningAlgorithm: variables.SigningAlgorithm,
		Key:              variables.TokenSignKey,
		VerifyKey:        variables.TokenVerifyKey,
		// Ten year refresh
		Timeout:    time.Hour * 24 * 365 * 10,
		MaxRefresh: time.Hour * 24 * 365 * 10,
		Authenticator: func(username string, password string) error {
			log.Println("Attempting login for", username)
			// FIXME: Make these passed in from viper
			if username != viper.GetString("username") ||
				password != viper.GetString("password") {
				return errors.New("Incorrect username and password")
			}
			return nil
		},
	}
	return authMiddleware, nil
}
func TestCall(t *testing.T) {
	err := os.Chdir("../../")
	if err != nil {
		log.Fatal(err)
	}

	handler := MakeHandler()

	// Use BackendToken to avoid login
	err = variables.LoadTokenKeys()
	if err != nil {
		t.Fatal(err)
	}
	// Test that call works
	path := variables.APIPathCallServer
	path = strings.Replace(path, ":number", "test number", 1)
	testReq := test.MakeSimpleRequest(
		"GET",
		"http://localhost"+path,
		nil,
	)
	testReq.Header.Set("Authorization", "Bearer "+variables.BackendToken)
	// Do not gzip the response
	testReq.Header.Set("Accept-Encoding", "identity")
	testRes := test.RunRequest(t, *handler, testReq)
	testRes.CodeIs(http.StatusOK)
	testRes.ContentTypeIsJson()
}