Esempio n. 1
0
func TestValidate(t *testing.T) {
	auth := auth.NewAuthorizer()

	if !auth.Validate(ExpectedToken) {
		t.Error("Expect to return true")
	}
}
Esempio n. 2
0
func TestNewAuthorizer(t *testing.T) {
	auth := auth.NewAuthorizer()

	if auth.Token != ExpectedToken {
		t.Error("Expected"+ExpectedToken+", got ", auth.Token)
	}
}
Esempio n. 3
0
func main() {
	authorizer := auth.NewAuthorizer()

	authConfig, err := conf.NewConfig("auth")
	if err != nil {
		fmt.Println("Error getting config", err)
		return
	}

	k := kitewrapper.NewKiteWrapper(authConfig)
	err = k.RegisterToKontrol()
	if err != nil {
		fmt.Println("Failed to register", err)
		return
	}

	var dbKite *kite.Client

	go func() {
		var err error

		dbKite, err = k.FindAndDial("db_accessor")
		if err != nil {
			fmt.Println("Failed to dial db service", err)
			// return
		}
	}()

	// Add our handler method
	k.HandleFunc("login", func(r *kite.Request) (interface{}, error) {
		var (
			params, _ = r.Args.One().Map()
			user, _   = params["user"].String()
			pass, _   = params["pass"].String()
		)

		if user == User && pass == Pass {
			return authorizer.Token, nil
		}

		return nil, BadCredentials
	})

	k.HandleFunc("profile", func(r *kite.Request) (interface{}, error) {
		token, err := r.Args.One().String()
		if err != nil {
			return nil, BadCredentials
		}

		if !authorizer.Validate(token) {
			return nil, BadCredentials
		}

		result, err := dbKite.Tell("query", "profile")
		if err != nil {
			return nil, err
		}

		var (
			profileMap, _ = result.Map()
			profile       = make(map[string]string)
		)
		for name, value := range profileMap {
			val, _ := value.String()
			profile[name] = val
		}

		return profile, nil
	})

	k.HandleFunc("validateToken", func(r *kite.Request) (interface{}, error) {
		token, err := r.Args.One().String()

		if err != nil {
			return false, BadCredentials
		}

		return authorizer.Validate(token), nil
	})

	k.Run()
}