Skip to content

ir-dev/ark-captcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ArkCaptcha

ArkCaptcha is a lightweight, self-hosted CAPTCHA solution for ASP.NET Core and modern web applications.

It provides:

  • πŸ” Server-side CAPTCHA generation (ASP.NET Core)
  • 🌐 Browser SDK (ark-captcha.js)
  • 🧩 Razor TagHelper (<ark-captcha>)
  • ⚑ Zero external dependencies (no Google reCAPTCHA)
  • 🏒 Enterprise-ready architecture (scalable & extensible)

πŸš€ Features

  • Image-based CAPTCHA with noise & distortion
  • Token-based validation (one-time use)
  • Auto-rendered UI (no HTML required)
  • Custom UI support
  • TagHelper integration for Razor
  • Built-in CSS injection (no external styles required)
  • Multi-instance safe (script loads only once)
  • Async-friendly API

πŸ“¦ Packages

Backend (NuGet)

dotnet add package ArkCaptcha.AspNetCore

Frontend (NPM / CDN)

npm install ark-captcha

OR

<script src="https://cdn.jsdelivr.net/npm/ark-captcha@latest/ark-captcha.js"></script>

πŸ—οΈ Architecture

Browser (ArkCaptcha JS / TagHelper)
        ↓
ASP.NET Core API (/ark/api/captcha)
        ↓
Token + Image
        ↓
User Input
        ↓
Validation (Server-side)

βš™οΈ ASP.NET Core Setup

1. Register Services

builder.Services.AddSingleton<ArkCaptchaService>();

2. Map Controller

app.MapControllers();

3. Default Endpoints

Endpoint Description
/ark/api/captcha Generate CAPTCHA
/ark/api/captcha/validate Validate CAPTCHA

πŸ§ͺ Usage Options


βœ… 1. Auto Render (Zero HTML)

<div id="captcha"></div>

<script>
new ArkCaptcha({
    container: document.getElementById("captcha"),
    apiUrl: "/ark/api/captcha",
    enableVerify: true
});
</script>

βœ” Fully automatic UI βœ” Built-in styling βœ” Optional verify button


βœ… 2. Custom UI Integration

<img id="img" />
<input id="input" />
<button id="refresh">Refresh</button>
<button id="submit">Validate</button>

<script>
const captcha = new ArkCaptcha({
    apiUrl: "/ark/api/captcha",
    imageElement: document.getElementById("img"),
    inputElement: document.getElementById("input"),
    refreshButton: document.getElementById("refresh")
});

document.getElementById("submit").onclick = async () => {
    const valid = await captcha.validate();
    console.log(valid);
};
</script>

βœ… 3. Razor TagHelper (Recommended for ASP.NET)

Usage

<ark-captcha api-url="/ark/api/captcha" enable-verify="true"></ark-captcha>

Features

  • Auto loads ark-captcha.js (once globally)
  • Supports multiple instances per page
  • Injects UI automatically
  • Exposes instance via DOM:
document.querySelector("#captcha_id").captcha

🧠 Access Value (Form Integration)

const data = await captcha.getValue();

/*
{
  token: "...",
  value: "USER_INPUT"
}
*/

πŸ” Server-Side Validation

[HttpPost]
public IActionResult Submit(FormModel model)
{
    var isValid = _captchaService.ValidateCaptcha(
        model.Token,
        model.Value
    );

    if (!isValid)
    {
        return BadRequest("Invalid Captcha");
    }

    return Ok();
}

πŸ“‘ API Contract

GET /ark/api/captcha

  • Returns: CAPTCHA image (PNG)
  • Header:
X-Ark-Captcha-Token: <token>

POST /ark/api/captcha/validate

{
  "token": "string",
  "value": "string"
}

Response:

{
  "success": true
}

🎨 UI Behavior

If no elements are provided:

  • ArkCaptcha auto-injects:

    • HTML
    • CSS
    • Interaction logic

Optional:

enableVerify: true

πŸ‘‰ Adds built-in Verify button


βš™οΈ Configuration Options

new ArkCaptcha({
    apiUrl: "/ark/api/captcha",
    container: HTMLElement,
    imageElement: HTMLElement,
    inputElement: HTMLElement,
    refreshButton: HTMLElement,
    enableVerify: true
});

🧠 Methods

loadCaptcha()

Refresh CAPTCHA image

validate()

const result = await captcha.validate();

Returns:

true | false

getValue()

const data = await captcha.getValue();

Returns:

{
  token,
  value
}

showSuccess() / showFailure()

Manually control UI state


πŸ”’ Security Features

  • One-time token validation
  • Token removed after validation
  • Case-insensitive comparison
  • No external tracking
  • Self-hosted (GDPR friendly)

πŸ“ Project Structure

ArkCaptcha/
 β”œβ”€β”€ ArkCaptcha.AspNetCore/
 β”‚   β”œβ”€β”€ ArkCaptchaService.cs
 β”‚   β”œβ”€β”€ CaptchaController.cs
 β”‚   └── CaptchaTagHelper.cs
 β”œβ”€β”€ ark-captcha.js
 β”œβ”€β”€ LICENSE
 └── README.md

βš–οΈ Licensing

  • MIT License (your code)
  • Uses BSD-3 licensed dependencies (e.g. SkiaSharp)

πŸ’‘ Best Practices

  • Use Redis / distributed cache instead of in-memory store
  • Add token expiry (5–10 minutes)
  • Enable rate limiting for /captcha endpoints
  • Use HTTPS in production
  • Avoid exposing token in URLs

πŸš€ Roadmap

  • Dark mode UI
  • Canvas-based CAPTCHA
  • Invisible CAPTCHA mode
  • Blazor component
  • Multi-language support

🀝 Contributing

Contributions are welcome!

  • Fork the repo
  • Create a feature branch
  • Submit a PR

⭐ Support

If you find ArkCaptcha useful, give it a ⭐ on GitHub!


About

simple captcha library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors