476 lines
18 KiB
JavaScript
476 lines
18 KiB
JavaScript
/**
|
|
* Slider Captcha Puzzle
|
|
* Simple and secure slider puzzle captcha implementation
|
|
*/
|
|
|
|
class SliderCaptcha {
|
|
constructor(options = {}) {
|
|
this.container = options.container || '#slider-captcha-container';
|
|
this.width = options.width || 300;
|
|
this.height = options.height || 150;
|
|
this.sliderSize = options.sliderSize || 50;
|
|
this.tolerance = options.tolerance || 5;
|
|
this.onSuccess = options.onSuccess || function() {};
|
|
this.onFail = options.onFail || function() {};
|
|
this.endpoint = options.endpoint || '/captcha/validate';
|
|
|
|
this.puzzleX = 0;
|
|
this.puzzleY = 0;
|
|
this.sliderX = 0;
|
|
this.isMoving = false;
|
|
this.verified = false;
|
|
this.token = null;
|
|
|
|
this.init();
|
|
}
|
|
|
|
init() {
|
|
this.createElements();
|
|
this.loadPuzzle();
|
|
this.attachEvents();
|
|
}
|
|
|
|
createElements() {
|
|
const container = document.querySelector(this.container);
|
|
if (!container) {
|
|
console.error('Slider captcha container not found');
|
|
return;
|
|
}
|
|
|
|
container.innerHTML = `
|
|
<div class="slider-captcha-wrapper" style="width: 100%;">
|
|
<div class="slider-captcha-canvas-wrapper" style="position: relative; width: 100%; height: ${this.height}px; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; background: #f5f5f5;">
|
|
<canvas id="captcha-canvas" width="${this.width}" height="${this.height}" style="display: block; width: 100%; height: 100%;"></canvas>
|
|
<canvas id="captcha-puzzle" width="${this.sliderSize * 2}" height="${this.height}" style="position: absolute; top: 0; left: 0; display: block;"></canvas>
|
|
<div class="slider-loading" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: none;">
|
|
<i class="fas fa-spinner fa-spin" style="font-size: 24px; color: #3b82f6;"></i>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="slider-control" style="margin-top: 15px; position: relative; height: 40px; background: #f0f9ff; border: 2px solid #dbeafe; border-radius: 10px; overflow: hidden; width: 100%;">
|
|
<div class="slider-track" style="position: absolute; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: #3b82f6; font-size: 14px; user-select: none; pointer-events: none;">
|
|
<i class="fas fa-arrows-alt-h" style="margin-right: 5px;"></i>
|
|
<span class="slider-text">Geser untuk verifikasi</span>
|
|
</div>
|
|
<div class="slider-button" style="position: absolute; left: 0; top: 0; width: 40px; height: 40px; background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 50%, #1d4ed8 100%); border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center; color: white; box-shadow: 0 2px 5px rgba(0,0,0,0.2); transition: transform 0.3s ease;">
|
|
<i class="fas fa-chevron-right"></i>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="slider-status" style="margin-top: 10px; font-size: 13px; text-align: center; height: 20px;"></div>
|
|
|
|
<button type="button" class="slider-refresh" style="margin-top: 10px; padding: 8px 15px; background: white; border: 2px solid #dbeafe; border-radius: 8px; color: #3b82f6; cursor: pointer; font-size: 13px; display: flex; align-items: center; justify-content: center; gap: 5px; transition: all 0.3s ease; width: 100%;">
|
|
<i class="fas fa-redo"></i> Muat Ulang
|
|
</button>
|
|
</div>
|
|
`;
|
|
|
|
this.canvas = document.getElementById('captcha-canvas');
|
|
this.puzzleCanvas = document.getElementById('captcha-puzzle');
|
|
this.sliderButton = container.querySelector('.slider-button');
|
|
this.sliderTrack = container.querySelector('.slider-track');
|
|
this.sliderStatus = container.querySelector('.slider-status');
|
|
this.sliderLoading = container.querySelector('.slider-loading');
|
|
this.sliderRefresh = container.querySelector('.slider-refresh');
|
|
this.sliderText = container.querySelector('.slider-text');
|
|
|
|
this.ctx = this.canvas.getContext('2d');
|
|
this.puzzleCtx = this.puzzleCanvas.getContext('2d');
|
|
}
|
|
|
|
async loadPuzzle() {
|
|
this.showLoading(true);
|
|
|
|
try {
|
|
// Generate random puzzle position
|
|
this.puzzleX = Math.floor(Math.random() * (this.width - this.sliderSize - 50)) + 50;
|
|
this.puzzleY = Math.floor(Math.random() * (this.height - this.sliderSize - 20)) + 10;
|
|
|
|
// Load random landscape image
|
|
const img = new Image();
|
|
img.crossOrigin = 'anonymous';
|
|
|
|
// Use Picsum Photos API for random landscape images
|
|
const randomId = Math.floor(Math.random() * 1000);
|
|
img.src = `https://picsum.photos/seed/${randomId}/${this.width}/${this.height}`;
|
|
|
|
// Wait for image to load
|
|
await new Promise((resolve, reject) => {
|
|
img.onload = resolve;
|
|
img.onerror = () => {
|
|
// Fallback to gradient if image fails to load
|
|
console.warn('Failed to load image, using gradient fallback');
|
|
this.drawGradientBackground();
|
|
resolve();
|
|
};
|
|
|
|
// Timeout after 5 seconds
|
|
setTimeout(() => {
|
|
if (!img.complete) {
|
|
console.warn('Image load timeout, using gradient fallback');
|
|
this.drawGradientBackground();
|
|
resolve();
|
|
}
|
|
}, 5000);
|
|
});
|
|
|
|
// Draw the image if it loaded successfully
|
|
if (img.complete && img.naturalWidth > 0) {
|
|
this.ctx.drawImage(img, 0, 0, this.width, this.height);
|
|
}
|
|
|
|
// Draw puzzle cutout on main canvas
|
|
this.drawPuzzleCutout(this.ctx, this.puzzleX, this.puzzleY, true);
|
|
|
|
// Draw puzzle piece
|
|
this.drawPuzzlePiece();
|
|
|
|
// Generate token for validation
|
|
this.token = this.generateToken();
|
|
|
|
} catch (error) {
|
|
console.error('Error loading puzzle:', error);
|
|
this.setStatus('Gagal memuat captcha', 'error');
|
|
}
|
|
|
|
this.showLoading(false);
|
|
}
|
|
|
|
drawGradientBackground() {
|
|
// Fallback gradient background
|
|
const gradient = this.ctx.createLinearGradient(0, 0, this.width, this.height);
|
|
gradient.addColorStop(0, '#667eea');
|
|
gradient.addColorStop(0.5, '#764ba2');
|
|
gradient.addColorStop(1, '#f093fb');
|
|
this.ctx.fillStyle = gradient;
|
|
this.ctx.fillRect(0, 0, this.width, this.height);
|
|
|
|
// Add some random shapes for complexity
|
|
for (let i = 0; i < 5; i++) {
|
|
const x = Math.random() * this.width;
|
|
const y = Math.random() * this.height;
|
|
const radius = Math.random() * 30 + 10;
|
|
this.ctx.beginPath();
|
|
this.ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
|
this.ctx.fillStyle = `rgba(255, 255, 255, ${Math.random() * 0.3})`;
|
|
this.ctx.fill();
|
|
}
|
|
}
|
|
|
|
drawPuzzleCutout(ctx, x, y, isCutout = false) {
|
|
const size = this.sliderSize;
|
|
const r = size / 4; // Radius untuk tonjolan/lekukan puzzle
|
|
|
|
ctx.save();
|
|
ctx.beginPath();
|
|
|
|
// Gambar bentuk puzzle klasik dengan tonjolan
|
|
// Mulai dari kiri atas
|
|
ctx.moveTo(x, y);
|
|
|
|
// Sisi atas dengan tonjolan
|
|
ctx.lineTo(x + size / 3, y);
|
|
ctx.arc(x + size / 2, y, r, Math.PI, 0, false); // Tonjolan atas
|
|
ctx.lineTo(x + size, y);
|
|
|
|
// Sisi kanan dengan tonjolan
|
|
ctx.lineTo(x + size, y + size / 3);
|
|
ctx.arc(x + size, y + size / 2, r, Math.PI * 1.5, Math.PI * 0.5, true); // Lekukan kanan
|
|
ctx.lineTo(x + size, y + size);
|
|
|
|
// Sisi bawah dengan tonjolan
|
|
ctx.lineTo(x + size * 2 / 3, y + size);
|
|
ctx.arc(x + size / 2, y + size, r, 0, Math.PI, true); // Lekukan bawah
|
|
ctx.lineTo(x, y + size);
|
|
|
|
// Sisi kiri dengan tonjolan
|
|
ctx.lineTo(x, y + size * 2 / 3);
|
|
ctx.arc(x, y + size / 2, r, Math.PI * 0.5, Math.PI * 1.5, false); // Tonjolan kiri
|
|
ctx.lineTo(x, y);
|
|
|
|
ctx.closePath();
|
|
|
|
if (isCutout) {
|
|
ctx.strokeStyle = 'rgba(255, 255, 255, 0.8)';
|
|
ctx.lineWidth = 2;
|
|
ctx.stroke();
|
|
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
|
ctx.fill();
|
|
}
|
|
|
|
ctx.restore();
|
|
}
|
|
|
|
drawPuzzlePiece() {
|
|
const size = this.sliderSize;
|
|
const r = size / 4;
|
|
const padding = r; // Padding untuk tonjolan
|
|
|
|
// Clear puzzle canvas
|
|
this.puzzleCtx.clearRect(0, 0, this.sliderSize * 2, this.height);
|
|
|
|
// Save original image data first before clipping
|
|
this.puzzleCtx.save();
|
|
|
|
// Draw the full image from main canvas first
|
|
const tempCanvas = document.createElement('canvas');
|
|
tempCanvas.width = size + r * 2; // Extra space for bulges
|
|
tempCanvas.height = size + r * 2;
|
|
const tempCtx = tempCanvas.getContext('2d');
|
|
|
|
// Copy image area (with extra padding for bulges)
|
|
const sourceX = Math.max(0, this.puzzleX - r);
|
|
const sourceY = Math.max(0, this.puzzleY - r);
|
|
const sourceWidth = Math.min(size + r * 2, this.width - sourceX);
|
|
const sourceHeight = Math.min(size + r * 2, this.height - sourceY);
|
|
|
|
tempCtx.drawImage(
|
|
this.canvas,
|
|
sourceX, sourceY, sourceWidth, sourceHeight,
|
|
0, 0, sourceWidth, sourceHeight
|
|
);
|
|
|
|
// Create clipping path dengan bentuk puzzle
|
|
const offsetX = this.puzzleX - sourceX;
|
|
const offsetY = this.puzzleY - sourceY;
|
|
|
|
this.puzzleCtx.beginPath();
|
|
|
|
// Draw exact same puzzle shape dengan padding di kiri
|
|
const x = padding; // Mulai dari padding, bukan 0
|
|
const y = this.puzzleY;
|
|
|
|
// Sisi atas dengan tonjolan
|
|
this.puzzleCtx.moveTo(x, y);
|
|
this.puzzleCtx.lineTo(x + size / 3, y);
|
|
this.puzzleCtx.arc(x + size / 2, y, r, Math.PI, 0, false);
|
|
this.puzzleCtx.lineTo(x + size, y);
|
|
|
|
// Sisi kanan dengan lekukan
|
|
this.puzzleCtx.lineTo(x + size, y + size / 3);
|
|
this.puzzleCtx.arc(x + size, y + size / 2, r, Math.PI * 1.5, Math.PI * 0.5, true);
|
|
this.puzzleCtx.lineTo(x + size, y + size);
|
|
|
|
// Sisi bawah dengan lekukan
|
|
this.puzzleCtx.lineTo(x + size * 2 / 3, y + size);
|
|
this.puzzleCtx.arc(x + size / 2, y + size, r, 0, Math.PI, true);
|
|
this.puzzleCtx.lineTo(x, y + size);
|
|
|
|
// Sisi kiri dengan tonjolan
|
|
this.puzzleCtx.lineTo(x, y + size * 2 / 3);
|
|
this.puzzleCtx.arc(x, y + size / 2, r, Math.PI * 0.5, Math.PI * 1.5, false);
|
|
this.puzzleCtx.lineTo(x, y);
|
|
|
|
this.puzzleCtx.closePath();
|
|
this.puzzleCtx.clip();
|
|
|
|
// Draw the temp canvas with proper offset
|
|
this.puzzleCtx.drawImage(
|
|
tempCanvas,
|
|
padding - offsetX, this.puzzleY - offsetY
|
|
);
|
|
|
|
this.puzzleCtx.restore();
|
|
|
|
// Draw border dengan bentuk puzzle
|
|
this.puzzleCtx.save();
|
|
this.puzzleCtx.beginPath();
|
|
|
|
// Gambar border yang sama
|
|
this.puzzleCtx.moveTo(x, y);
|
|
this.puzzleCtx.lineTo(x + size / 3, y);
|
|
this.puzzleCtx.arc(x + size / 2, y, r, Math.PI, 0, false);
|
|
this.puzzleCtx.lineTo(x + size, y);
|
|
this.puzzleCtx.lineTo(x + size, y + size / 3);
|
|
this.puzzleCtx.arc(x + size, y + size / 2, r, Math.PI * 1.5, Math.PI * 0.5, true);
|
|
this.puzzleCtx.lineTo(x + size, y + size);
|
|
this.puzzleCtx.lineTo(x + size * 2 / 3, y + size);
|
|
this.puzzleCtx.arc(x + size / 2, y + size, r, 0, Math.PI, true);
|
|
this.puzzleCtx.lineTo(x, y + size);
|
|
this.puzzleCtx.lineTo(x, y + size * 2 / 3);
|
|
this.puzzleCtx.arc(x, y + size / 2, r, Math.PI * 0.5, Math.PI * 1.5, false);
|
|
this.puzzleCtx.lineTo(x, y);
|
|
|
|
this.puzzleCtx.strokeStyle = 'rgba(255, 255, 255, 0.8)';
|
|
this.puzzleCtx.lineWidth = 2;
|
|
this.puzzleCtx.stroke();
|
|
this.puzzleCtx.restore();
|
|
}
|
|
|
|
attachEvents() {
|
|
const self = this;
|
|
|
|
// Mouse events
|
|
this.sliderButton.addEventListener('mousedown', (e) => {
|
|
if (!self.verified) {
|
|
self.startSlide(e);
|
|
}
|
|
});
|
|
|
|
document.addEventListener('mousemove', (e) => {
|
|
if (self.isMoving) {
|
|
self.moveSlide(e);
|
|
}
|
|
});
|
|
|
|
document.addEventListener('mouseup', (e) => {
|
|
if (self.isMoving) {
|
|
self.endSlide(e);
|
|
}
|
|
});
|
|
|
|
// Touch events for mobile
|
|
this.sliderButton.addEventListener('touchstart', (e) => {
|
|
if (!self.verified) {
|
|
self.startSlide(e.touches[0]);
|
|
}
|
|
});
|
|
|
|
document.addEventListener('touchmove', (e) => {
|
|
if (self.isMoving) {
|
|
self.moveSlide(e.touches[0]);
|
|
}
|
|
});
|
|
|
|
document.addEventListener('touchend', (e) => {
|
|
if (self.isMoving) {
|
|
self.endSlide(e.changedTouches[0]);
|
|
}
|
|
});
|
|
|
|
// Refresh button
|
|
this.sliderRefresh.addEventListener('click', () => {
|
|
self.reset();
|
|
});
|
|
}
|
|
|
|
startSlide(e) {
|
|
this.isMoving = true;
|
|
this.startX = e.clientX || e.pageX;
|
|
this.sliderButton.style.transition = 'none';
|
|
this.puzzleCanvas.style.transition = 'none';
|
|
this.sliderStatus.textContent = '';
|
|
}
|
|
|
|
moveSlide(e) {
|
|
const currentX = e.clientX || e.pageX;
|
|
let moveX = currentX - this.startX;
|
|
|
|
// Constrain movement
|
|
const maxMove = this.width - 40;
|
|
moveX = Math.max(0, Math.min(moveX, maxMove));
|
|
|
|
this.sliderX = moveX;
|
|
this.sliderButton.style.left = moveX + 'px';
|
|
|
|
// Offset puzzle canvas dengan padding untuk tonjolan kiri
|
|
const padding = this.sliderSize / 4;
|
|
this.puzzleCanvas.style.left = (moveX - padding) + 'px';
|
|
}
|
|
|
|
endSlide(e) {
|
|
this.isMoving = false;
|
|
this.sliderButton.style.transition = 'transform 0.3s ease';
|
|
|
|
// Check if puzzle piece is in correct position
|
|
const diff = Math.abs(this.sliderX - this.puzzleX);
|
|
|
|
if (diff <= this.tolerance) {
|
|
this.onVerifySuccess();
|
|
} else {
|
|
this.onVerifyFail();
|
|
}
|
|
}
|
|
|
|
async onVerifySuccess() {
|
|
this.verified = true;
|
|
this.sliderButton.innerHTML = '<i class="fas fa-check"></i>';
|
|
this.sliderButton.style.background = 'linear-gradient(135deg, #059669 0%, #10b981 100%)';
|
|
this.sliderTrack.style.background = '#d1fae5';
|
|
this.sliderText.textContent = 'Verifikasi berhasil!';
|
|
this.sliderText.style.color = '#059669';
|
|
this.setStatus('✓ Verifikasi berhasil!', 'success');
|
|
|
|
// Store validation token
|
|
sessionStorage.setItem('captcha_token', this.token);
|
|
sessionStorage.setItem('captcha_verified', 'true');
|
|
|
|
// Call success callback
|
|
this.onSuccess(this.token);
|
|
}
|
|
|
|
onVerifyFail() {
|
|
this.sliderButton.style.background = 'linear-gradient(135deg, #dc2626 0%, #ef4444 100%)';
|
|
this.sliderTrack.style.background = '#fee2e2';
|
|
this.setStatus('✗ Verifikasi gagal, coba lagi', 'error');
|
|
|
|
// Call fail callback
|
|
this.onFail();
|
|
|
|
// Add smooth transition and reset to initial position
|
|
setTimeout(() => {
|
|
// Enable smooth transition
|
|
this.sliderButton.style.transition = 'left 0.5s ease, transform 0.3s ease';
|
|
this.puzzleCanvas.style.transition = 'left 0.5s ease';
|
|
|
|
// Reset position with animation (puzzle canvas ke posisi initial dengan offset padding)
|
|
const padding = this.sliderSize / 4;
|
|
this.sliderButton.style.left = '0px';
|
|
this.puzzleCanvas.style.left = (-padding) + 'px';
|
|
|
|
// Reset after animation completes
|
|
setTimeout(() => {
|
|
this.reset();
|
|
}, 500);
|
|
}, 800);
|
|
}
|
|
|
|
setStatus(message, type) {
|
|
this.sliderStatus.textContent = message;
|
|
this.sliderStatus.style.color = type === 'success' ? '#059669' : type === 'error' ? '#dc2626' : '#3b82f6';
|
|
}
|
|
|
|
showLoading(show) {
|
|
if (this.sliderLoading) {
|
|
this.sliderLoading.style.display = show ? 'block' : 'none';
|
|
}
|
|
}
|
|
|
|
reset() {
|
|
this.verified = false;
|
|
this.sliderX = 0;
|
|
this.sliderButton.style.left = '0px';
|
|
this.sliderButton.style.background = 'linear-gradient(135deg, #1e3a8a 0%, #3b82f6 50%, #1d4ed8 100%)';
|
|
this.sliderButton.innerHTML = '<i class="fas fa-chevron-right"></i>';
|
|
this.sliderTrack.style.background = '#f0f9ff';
|
|
this.sliderText.textContent = 'Geser untuk verifikasi';
|
|
this.sliderText.style.color = '#3b82f6';
|
|
this.sliderStatus.textContent = '';
|
|
|
|
// Reset puzzle canvas ke posisi initial dengan offset padding
|
|
const padding = this.sliderSize / 4;
|
|
this.puzzleCanvas.style.left = (-padding) + 'px';
|
|
|
|
// Clear session storage
|
|
sessionStorage.removeItem('captcha_token');
|
|
sessionStorage.removeItem('captcha_verified');
|
|
|
|
this.loadPuzzle();
|
|
}
|
|
|
|
generateToken() {
|
|
return 'captcha_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
|
|
}
|
|
|
|
isVerified() {
|
|
return this.verified && sessionStorage.getItem('captcha_verified') === 'true';
|
|
}
|
|
|
|
getToken() {
|
|
return this.token || sessionStorage.getItem('captcha_token');
|
|
}
|
|
}
|
|
|
|
// Export for use in other scripts
|
|
window.SliderCaptcha = SliderCaptcha;
|