init kpass
nexler init kpass
local only
Adds a client for KPass (Klivolks' permission-check service) to an existing app: kpass/kpass.go (Check/Allowed/Authorise, plus UserIDFromRequest when the app has -auth jwt|session|both), a one-time-written wrapper at services/kpass/kpass.go (CheckAccess(ctx, userID, resource, extra) (bool, error) — call this from your handlers/services instead of kpass.Check directly, so every authorization decision funnels through one place you can extend later), and KPASS_URL/KPASS_CLIENT_ID/KPASS_API_SECRET added to .env (blank, for you to fill in) if not already present.
Check(ctx, userID, resource, extra) posts to {KPASS_URL}/permission/check/ and returns KPass's full response (status, message, access, user role, user type) — kept separate from Allowed (a plain yes/no helper) so callers needing more, e.g. restricting data by role, call Check directly. Authorise(ctx, code) is a separate capability: it GETs {KPASS_URL}/api/v1/authorise?code=...&client_id=... (query params, not headers — api_secret only sent if set) to resolve a one-time login/auth code from the frontend into the user's identity, returning a flat AuthoriseResult{UserId, UserType, UserName, UserRole, OrgId}. Unlike init db, this needs no network access — pure local file scaffolding, safe to re-run.
nexler init kpass -dir ./myapp
Usage — call services/kpass's CheckAccess from any handler you want permission-gated, typically right after (or instead of) middleware.RequireAuth. Need UserRole/UserType for finer-grained checks instead of a plain bool? Call kpass.Check directly (or add another function to services/kpass that does):
func HandleUsersCreate(w http.ResponseWriter, r *http.Request) {
userID, ok := kpass.UserIDFromRequest(r) // thin wrapper over auth.Subject(r)
if !ok {
response.Error(w, r, http.StatusUnauthorized, "not authenticated")
return
}
allowed, err := kpasssvc.CheckAccess(r.Context(), userID, "myapp.users.create", nil)
if err != nil || !allowed {
response.Error(w, r, http.StatusForbidden, "not allowed")
return
}
// ... proceed with the real handler logic
}A login-style handler that trades a frontend-supplied code for the user's identity:
func HandleLoginPost(w http.ResponseWriter, r *http.Request) {
var req loginmodels.PostLoginRequest // req.Code from the frontend
if err := request.DecodeJSON(r, &req); err != nil {
response.Error(w, r, http.StatusBadRequest, err.Error())
return
}
identity, err := kpass.Authorise(r.Context(), req.Code)
if err != nil {
response.Error(w, r, http.StatusUnauthorized, "invalid code")
return
}
// identity.UserId/UserName/UserRole/UserType/OrgId — start a session or issue a JWT here
}