Giter Site home page Giter Site logo

lipsarhq / go-telegram-login-widget Goto Github PK

View Code? Open in Web Editor NEW
3.0 0.0 0.0 11 KB

Telegram Login Widget library for Go/Golang.

License: MIT License

Makefile 0.75% Go 99.25%
go go-lib go-library golang telegram telegram-go telegram-login telegram-login-widget

go-telegram-login-widget's Introduction

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

Links

go-telegram-login-widget's People

Contributors

lipsarhq-klim avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.