telegramloginwidget

package module
v0.0.0-...-ec628ca Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 9 Imported by: 0

README

Telegram Login Widget

⭐ Star us on GitHub — it motivates us a lot!

Telegram Login Widget library for Go/Golang.

Table of Content

Usage/Examples

Unmarshal JSON
package main

import (
	"encoding/json"
)

// Telegram bot token.
const token = "XXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXX"

func main() {
	// 1. Get "AuthorizationData" from JSON.
	data := []byte(`{"first_name":"Klim","hash":"b7a7fc776729077786e4190aec2c5dcecd2ec66ae0faf1b44316d541b955da95","last_name":"Sidorov","photo_url":"https://t.me/klimsidorov","username":"klimsidorov","auth_date":976255200,"id":1}`)

	var modelAuthorizationData telegramloginwidget.AuthorizationData

	if err := json.Unmarshal(data, &modelAuthorizationData); err != nil {
		return
	}

	// 2. Call "Check" method to validate hash.
	if err := modelAuthorizationData.Check(token); err != nil {
		// Invalid hash.
		return
	}

	// Hash is valid.
}
NewFromQuery
package main

import (
	"net/url"

	"github.com/LipsarHQ/go-telegram-login-widget"
)

// Telegram bot token.
const token = "XXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXX"

func main() {
	// 1. Get "AuthorizationData" from url.Values.
	values := url.Values{
		"auth_date":  []string{"976255200"},
		"first_name": []string{"Klim"},
		"hash":       []string{"b7a7fc776729077786e4190aec2c5dcecd2ec66ae0faf1b44316d541b955da95"},
		"id":         []string{"1"},
		"last_name":  []string{"Sidorov"},
		"photo_url":  []string{"https://t.me/klimsidorov"},
		"username":   []string{"klimsidorov"},
	}

	modelAuthorizationData, err := telegramloginwidget.NewFromQuery(values)
	if err != nil {
		return
	}

	// 2. Call "Check" method to validate hash.
	if err = modelAuthorizationData.Check(token); err != nil {
		// Invalid hash.
		return
	}

	// Hash is valid.
}
NewFromURI
package main

import (
	"github.com/LipsarHQ/go-telegram-login-widget"
)

// Telegram bot token.
const token = "XXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXX"

func main() {
	// 1. Get "AuthorizationData" from uri.
	const uri = "https://example.com/?auth_date=976255200&first_name=Klim&hash=b7a7fc776729077786e4190aec2c5dcecd2ec66ae0faf1b44316d541b955da95&id=1&last_name=Sidorov&photo_url=https%3A%2F%2Ft.me%2Fklimsidorov&username=klimsidorov"

	modelAuthorizationData, err := telegramloginwidget.NewFromURI(uri)
	if err != nil {
		return
	}

	// 2. Call "Check" method to validate hash.
	if err = modelAuthorizationData.Check(token); err != nil {
		// Invalid hash.
		return
	}

	// Hash is valid.
}

FAQ

What is Telegram Login Widget?

The Telegram login widget is a simple way to authorize users on your website. Check out this page for a general overview of the widget.

How to validate hash?

Call Check method on AuthorizationData struct to validate hash.

How to calculate hash (for debug purpose)?

Call Sum method on AuthorizationData struct to calculate hash.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrHashInvalid = errors.New("hash_invalid")

Functions

This section is empty.

Types

type AuthorizationData

type AuthorizationData struct {
	FirstName string `json:"first_name,omitempty"`
	Hash      string `json:"hash,omitempty"`
	LastName  string `json:"last_name,omitempty"`
	PhotoURL  string `json:"photo_url,omitempty"`
	Username  string `json:"username,omitempty"`
	AuthDate  int64  `json:"auth_date,omitempty"`
	ID        int64  `json:"id,omitempty"`
}

func NewFromQuery

func NewFromQuery(values url.Values) (modelAuthorizationData *AuthorizationData, err error)

func NewFromURI

func NewFromURI(uri string) (modelAuthorizationData *AuthorizationData, err error)

func (*AuthorizationData) Check

func (x *AuthorizationData) Check(token string) (err error)

func (*AuthorizationData) String

func (x *AuthorizationData) String() string

func (*AuthorizationData) Sum

func (x *AuthorizationData) Sum(token string) string

Jump to

Keyboard shortcuts

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