https://github.com/9ssi7/sudo
traditional sudo mode feature to your golang backend/microservice
https://github.com/9ssi7/sudo
2fa-security golang security sudo
Last synced: about 2 months ago
JSON representation
traditional sudo mode feature to your golang backend/microservice
- Host: GitHub
- URL: https://github.com/9ssi7/sudo
- Owner: 9ssi7
- License: apache-2.0
- Created: 2024-03-01T18:22:30.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-01T19:08:14.000Z (about 1 year ago)
- Last Synced: 2025-03-28T15:56:15.157Z (about 2 months ago)
- Topics: 2fa-security, golang, security, sudo
- Language: Go
- Homepage: https://pkg.go.dev/github.com/9ssi7/sudo
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### sudo
The `sudo` package provides functionality for managing sudo-like operations, including user verification, token generation, and verification code validation.
### Installation
To install the `sudo` package, use the following `go get` command:
```bash
go get -u github.com/9ssi7/sudo
```### Usage
Import the `sudo` package into your code:
```go
import "github.com/9ssi7/sudo"
```### Configuration
Create a `sudo.Config` instance to configure the `sudo` service:
```go
config := sudo.Config{
Redis: // Your Redis service instance,
NotifySender: // Your NotifySender function,
Expire: // Optional: Set the expiration time for verification codes. Default is 5 minutes.
}sudoService := sudo.New(config)
```### Service Methods
#### `Check`
Check verifies the validity of a given token.
```go
cmd := sudo.CheckCommand{
UserId: "user123",
DeviceId: "device456",
Token: "your_token",
}err := sudoService.Check(context.Background(), cmd)
```#### `Start`
Start initiates the sudo process by generating a verification code and notifying the user.
```go
cmd := sudo.StartCommand{
UserId: "user123",
DeviceId: "device456",
Phone: "+1234567890",
Email: "[email protected]",
Locale: "en_US",
}token, err := sudoService.Start(context.Background(), cmd)
```#### `Verify`
Verify validates a user's input against the generated verification code.
```go
cmd := sudo.VerifyCommand{
UserId: "user123",
DeviceId: "device456",
VerifyToken: "generated_verify_token",
Code: "user_input_code",
}accessToken, err := sudoService.Verify(context.Background(), cmd)
```### Notifications
The package requires a notification sender function (`NotifySender`) to notify users during the sudo process.
```go
notifyFunc := func(cmd sudo.NotifyCommand) {
// Implement your notification logic here
}config.NotifySender = notifyFunc
```### Error Messages
The package provides the following error messages:
- `sudo_redis_fetch_failed`
- `sudo_redis_set_failed`
- `sudo_json_marshal_failed`
- `sudo_not_found`
- `sudo_invalid_token`
- `sudo_invalid_code`
- `sudo_expired_code`
- `sudo_exceed_try_count`
- `sudo_unknown`
- `sudo_verify_started`### Example
For a complete working examples, refer to the [recipes](github.com/9ssi7/recipes) repository.
### License
This package is licensed under the [Apache-2.0 License](LICENSE).