sudo

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

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:

go get -u github.com/9ssi7/sudo

Usage

Import the sudo package into your code:

import "github.com/9ssi7/sudo"

Configuration

Create a sudo.Config instance to configure the sudo service:

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.

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.

cmd := sudo.StartCommand{
    UserId:   "user123",
    DeviceId: "device456",
    Phone:    "+1234567890",
    Email:    "user@example.com",
    Locale:   "en_US",
}

token, err := sudoService.Start(context.Background(), cmd)

Verify

Verify validates a user's input against the generated verification code.

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.

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 repository.

License

This package is licensed under the Apache-2.0 License.

Documentation

Overview

Package sudo provides a service for handling sudo operations.

Package sudo provides error messages for the sudo package.

Package sudo provides a service for handling sudo operations.

Package sudo provides a service for handling sudo operations.

Package sudo provides a service for handling sudo operations.

Package sudo provides a service for handling sudo operations.

Index

Constants

This section is empty.

Variables

View Source
var Messages = messages{
	FailedRedisFetch: "sudo_redis_fetch_failed",
	FailedRedisSet:   "sudo_redis_set_failed",
	FailedMarshal:    "sudo_json_marshal_failed",
	NotFound:         "sudo_not_found",
	InvalidToken:     "sudo_invalid_token",
	InvalidCode:      "sudo_invalid_code",
	ExpiredCode:      "sudo_expired_code",
	ExceedTryCount:   "sudo_exceed_try_count",
	Unknown:          "sudo_unknown",
	VerifyStarted:    "sudo_verify_started",
}

Messages is an instance of the messages struct with predefined error messages.

Functions

This section is empty.

Types

type CheckCommand

type CheckCommand struct {
	// UserId is the unique identifier of the user.
	UserId string

	// DeviceId is the unique identifier of the device.
	DeviceId string

	// Token is the access token to be checked.
	Token string
}

CheckCommand represents the command structure for the Check operation.

type Config

type Config struct {
	// Redis is the Redis service.
	Redis mredis.Service

	// NotifySender is the function used to send notifications.
	NotifySender NotifySender

	// Expire is the expiration time for sudo operations.
	Expire time.Duration
}

Config holds the configuration for the sudo service.

type NotifyCommand

type NotifyCommand struct {
	// DeviceId is the unique identifier of the device.
	DeviceId string

	// Code is the verification code to be sent.
	Code string

	// Phone is the phone number to send the code to.
	Phone string

	// Email is the email address to send the code to.
	Email string

	// Locale is the language and region to use for the notification.
	Locale string
}

NotifyCommand represents the command structure for sending notifications.

type NotifySender

type NotifySender func(NotifyCommand)

NotifySender is a function type for sending notifications.

type Service

type Service interface {
	// Check validates the provided token for a given user and device.
	Check(ctx context.Context, cmd CheckCommand) error

	// Start initiates the sudo operation for a given user and device.
	Start(ctx context.Context, cmd StartCommand) (*string, error)

	// Verify validates the provided code and verify token for a given user and device.
	Verify(ctx context.Context, cmd VerifyCommand) (*string, error)

	// VerifyToken validates the format of a given token.
	VerifyToken(token string) error
}

Service defines the interface for the sudo service.

func New

func New(cnf Config) Service

New creates a new instance of the sudo service.

type StartCommand

type StartCommand struct {
	// UserId is the unique identifier of the user.
	UserId string

	// DeviceId is the unique identifier of the device.
	DeviceId string

	// Phone is the phone number to send the code to.
	Phone string

	// Email is the email address to send the code to.
	Email string

	// Locale is the language and region to use for the notification.
	Locale string
}

StartCommand represents the command structure for the Start operation.

type VerifyCommand

type VerifyCommand struct {
	// UserId is the unique identifier of the user.
	UserId string

	// DeviceId is the unique identifier of the device.
	DeviceId string

	// VerifyToken is the token to be verified.
	VerifyToken string

	// Code is the verification code to be verified.
	Code string
}

VerifyCommand represents the command structure for the Verify operation.

Directories

Path Synopsis
middleware

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL