teleauth

package module
v0.0.0-...-26361cb Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: BSD-3-Clause Imports: 17 Imported by: 0

README

Telegram authenticator

The telegram authenticator is a simple go package that allows you to authenticate users using Telegram bot.

How to use the authenticator

To use the authenticator, you need to create a Telegram bot and get the bot token. Then you can use the authenticator to authenticate users using the bot token and the telegram username of the user. There is next steps to authenticate user:

  • First time User should type /start command to Telegram bot
  • User select its telegram username in web or application form
  • Username sends to teleauth server by POST request
  • Teleauth server sends usernames chat_id to Telegram bot
  • Telegram bot sends authentification code to users telegram chat
  • User enters authentification code in web or application form
  • Authentification code sends to teleauth server by POST request
  • Teleauth server checks authentification code and returns success and token if code is valid

Teleauth admin application

There is admin panel application for teleauth server. You can use it to manage users. Set your teleauth server url in config.json and run go run ./cmd/client from main project directory.

Teleauth server application

There is teleauth server application with default teleauth server. You can use it to authenticate users. Set your telegram bot token in config.json and run go run ./cmd/server from main project directory.

License

BSD

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigPath

func ConfigPath(appConfigGroup, appShortName, fileName string) (string, error)

ConfigPath returns the path to a configuration file. The path is as:

os.UserConfigDir() + "appConfigGroup/appShortName/fileName"

Usually this function is used to get the path to config file in the same directory as the database file is stored.

Types

type AuthStartRequest

type AuthStartRequest struct {
	Username string `json:"username"`
}

AuthStartRequest is a request for starting the authentication process.

type AuthVerifyRequest

type AuthVerifyRequest struct {
	Username string `json:"username"`
	Code     string `json:"code"`
}

AuthVerifyRequest is a request for verifying the authentication code.

type DB

type DB struct {
	*sql.DB
}

DB represents the application's database.

func (*DB) GetChatID

func (db *DB) GetChatID(username string) (chatID int, err error)

GetChatID retrieves a chatid for a given username.

func (*DB) StoreUser

func (db *DB) StoreUser(username string, chatID int) error

StoreUser saves or updates a user's chat_id.

type Database

type Database interface {
	StoreUser(username string, chatID int) error
}

Database is an interface for storing and retrieving user data.

type Teleauth

type Teleauth struct {
	// contains filtered or unexported fields
}

Teleauth holds telegram bot token and authentification codes

func New

func New(token, appConfigGroup, appShortName string) (t *Teleauth, err error)

New creates a new Teleauth. Returns a new Teleauth or an error. Parameters:

  • token: Telegram bot token
  • appConfigGroup: Configuration group name
  • appShortName: Application short name

The appConfigGroup and appShortName are used to get the path to the database file. The database file is "teleauth.db" and is created if it does not exist. The path to the database file make as:

os.UserConfigDir() + "appConfigGroup/appShortName/teleauth.db"

func (*Teleauth) HandleAuthStart

func (t *Teleauth) HandleAuthStart(w http.ResponseWriter, r *http.Request)

HandleAuthStart is a HTTP handler for starting the authentication process.

It expects a JSON body with a single field "username". If the request method is not POST or the body is invalid, it returns an error response.

If the username is not found in the database, it returns a 404 status code with an error message.

If the database query fails, it returns a 500 status code with an error message.

If the code sending fails, it returns a 500 status code with an error message.

On success, it returns a 200 status code with a success message.

func (*Teleauth) HandleAuthVerify

func (t *Teleauth) HandleAuthVerify(w http.ResponseWriter, r *http.Request)

HandleAuthVerify is a HTTP handler for verifying the authentication code.

It expects a JSON body with two fields: "username" and "code". If the request method is not POST or the body is invalid, it returns an error response.

If the username is not found in the database, it returns a 404 status code with an error message.

If the database query fails, it returns a 500 status code with an error message.

If the code is invalid or expired, it returns a 401 status code with an error message.

On success, it returns a 200 status code with a success message and a real API token/key.

type Users

type Users struct {
	Username string `db:"username" db_key:"not null primary key"`
	ChatID   int    `db:"chatid" db_key:"not null unique"`
}

Users represents the users table.

Jump to

Keyboard shortcuts

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