@@ -18,6 +18,14 @@ type Service struct {
1818 logger * zap.Logger
1919}
2020
21+ // NewService returns a new OTP service.
22+ //
23+ // It takes the configuration for the OTP service,
24+ // a storage interface for storing the OTP codes,
25+ // and a logger for logging events.
26+ //
27+ // It returns an error if the configuration is invalid,
28+ // or if either the storage or logger is nil.
2129func NewService (cfg Config , storage * Storage , logger * zap.Logger ) (* Service , error ) {
2230 if err := cfg .Validate (); err != nil {
2331 return nil , err
@@ -34,6 +42,14 @@ func NewService(cfg Config, storage *Storage, logger *zap.Logger) (*Service, err
3442 return & Service {cfg : cfg , storage : storage , logger : logger }, nil
3543}
3644
45+ // Generate generates a new one-time user authorization code.
46+ //
47+ // It takes a context and the ID of the user for whom the code is being generated.
48+ //
49+ // It returns the generated code and an error. If the code can't be generated
50+ // within the configured number of retries, it returns an error.
51+ //
52+ // The generated code is stored with a TTL of the configured duration.
3753func (s * Service ) Generate (ctx context.Context , userID string ) (* Code , error ) {
3854 var code string
3955 var err error
@@ -65,6 +81,14 @@ func (s *Service) Generate(ctx context.Context, userID string) (*Code, error) {
6581 return & Code {Code : code , ValidUntil : validUntil }, nil
6682}
6783
84+ // Validate validates a one-time user authorization code.
85+ //
86+ // It takes a context and the one-time code to be validated.
87+ //
88+ // It returns the user ID associated with the code, and an error.
89+ // If the code is invalid, it returns ErrKeyNotFound.
90+ // If there is an error while validating the code, it returns the error.
91+ // If the code is valid, it deletes the code from the storage and returns the user ID.
6892func (s * Service ) Validate (ctx context.Context , code string ) (string , error ) {
6993 return s .storage .GetAndDelete (ctx , code )
7094}
0 commit comments