Overview
One-time links provide maximum security for sensitive file sharing by becoming permanently inaccessible after a single use. This feature is ideal for sharing confidential documents, temporary passwords, or any content that should only be accessed once.How One-Time Links Work
One-time links use a two-flag system to track usage:- Create access link with
oneTimeUse: true - First access succeeds and sets
used: true - All subsequent access attempts are denied
- Link remains in database but becomes permanently inaccessible
The
used flag is set after successful access, not during the check. This ensures the first access is always granted.Data Model
One-time use is controlled by two boolean fields in the Access model (seemodels/access.go:15-16):
OneTimeUse- Enables single-use behavior when set totrueUsed- Tracks whether the link has been accessed; set totrueafter first use
Creating One-Time Links
Configure additional restrictions
Optionally add IP restrictions, expiration, or other security measures.
API Examples
Success Response
OneTimeUse: true and Used: false - the link is ready for its single use.
Validation Logic
One-time use is validated in two stages during the access middleware (seemiddleware/access_restrictions.go:52-55 and 73-80):
Stage 1: Pre-Access Check
- Check if
OneTimeUseis true ANDUsedis true - If both true, deny access (link already used)
- If
Usedis false, allow access to proceed
Stage 2: Post-Access Marking
- After all checks pass and access is granted
- If
OneTimeUseis true, setUsedto true - Save the updated access record to database
- All future attempts will fail the pre-access check
Lifecycle Example
Let’s trace a one-time link through its complete lifecycle:First Access Attempt
User clicks the link for the first time.Pre-access check:Flag is immediately set to
OneTimeUse: true✓Used: false✓- Check passes, access granted
Used: true.Second Access Attempt
Same or different user tries to access the link.Pre-access check:
OneTimeUse: true✓Used: true✗- Check fails, access denied
Error Response
When attempting to access a used one-time link:403 Forbidden
This error is permanent - once a one-time link is used, it cannot be reset through normal access.
Monitoring Link Usage
You can check if a one-time link has been used:Used: true field indicates the link has been accessed.
Resetting Used Links
If you need to restore a used one-time link (e.g., first access failed to download), you can reset theused flag:
Updating an access record through the API does not automatically reset the
used flag. The flag persists unless the access record is deleted and recreated.Combining with Other Restrictions
One-time links can be layered with other access controls for enhanced security:One-Time + Expiration
- Link expires after first use, OR
- Link expires on date (whichever comes first)
One-Time + IP Restrictions
- Access only from allowed IPs/subnets, AND
- Only once from any of those addresses
One-Time + TTL (Not Recommended)
One-Time vs TTL
Both limit access, but work differently:| Feature | One-Time Use | TTL |
|---|---|---|
| Uses allowed | 1 | Configurable (any integer) |
| Configuration | oneTimeUse: true | enableTTL: true + ttl: N |
| Tracking | used boolean flag | Integer counter |
| Reset behavior | Cannot be reset (must delete) | Can be increased |
| When marked | After first access | Decrements during access |
| Best for | Maximum security, sensitive files | Multi-use limits, trial access |
Use Cases
Password Sharing
Share temporary credentials securely:Legal Documents
Share contracts or NDAs for review:Secure File Transfers
Exchange sensitive files with partners:Audit Logs
Provide one-time access to audit reports:Access Restrictions Order
The middleware checks restrictions in a specific order (seemiddleware/access_restrictions.go:44-50):
- Subnet Restriction - Verify client IP in allowed subnets
- IP Restriction - Verify client IP in allowed IPs
- One-Time Use - Check if link already used
- TTL - Decrement and check TTL counter
- Expiration - Verify current time before expiration
If any check fails, subsequent checks are not performed and access is immediately denied.
Best Practices
- Use for Sensitive Content - Reserve one-time links for truly confidential information
- Add Expiration Dates - Combine with time limits to prevent indefinite unused links
- Layer IP Restrictions - Add IP/subnet filters for maximum security
- Test Before Sharing - Verify the link works, but remember it will be consumed
- Monitor Usage - Check
usedflag to confirm recipient accessed the file - Create New for Retries - If first access fails, delete and recreate the link
- Avoid TTL Combination - Don’t combine with TTL; use one or the other
- Document Purpose - Use descriptive names to track why one-time was needed
Security Considerations
For maximum security, combine one-time links with:- IP restrictions - Ensure only specific IP can access
- Expiration - Limit the window of opportunity
- Out-of-band communication - Share link via secure channel
Troubleshooting
”Link already used” but recipient never accessed
Possible causes:- Link preview bot/crawler accessed the link
- Accidental click during testing
- Malicious actor intercepted the link
Need to allow second access
One-time links are permanent by design. Solution: Create a new one-time link or a TTL link withttl: 2.
Monitoring who accessed
DefDrive doesn’t log access timestamps or IPs in the access record. Solution: Check server logs or implement custom logging middleware.Next Steps
- TTL Configuration - Limit access by number of uses
- Creating Access Links - Learn about other access restrictions
- IP Subnet Restrictions - Control access by network location