vault

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package vault contains gotd secret storage implementations using Hashicorp Vault.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Credentials

type Credentials struct {
	kv.Credentials
}

Credentials stores user credentials to Vault.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"
	"github.com/hashicorp/vault/api"

	"github.com/gotd/td/telegram"
	tgauth "github.com/gotd/td/telegram/auth"

	"github.com/gotd/contrib/auth"
	"github.com/gotd/contrib/auth/terminal"
	"github.com/gotd/contrib/vault"
)

func vaultAuth(ctx context.Context) error {
	vaultClient, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		return errors.Errorf("create Vault client: %w", err)
	}
	cred := vault.NewCredentials(vaultClient, "cubbyhole/telegram/user").
		WithPhoneKey("phone").
		WithPasswordKey("password")

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		return client.Auth().IfNecessary(
			ctx,
			tgauth.NewFlow(auth.Build(cred, terminal.OS()), tgauth.SendCodeOptions{}),
		)
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := vaultAuth(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}
Output:

func NewCredentials

func NewCredentials(client *api.Client, path string) Credentials

NewCredentials creates new Credentials.

type SessionStorage

type SessionStorage struct {
	kv.Session
}

SessionStorage is a MTProto session Vault storage.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"
	"github.com/hashicorp/vault/api"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/vault"
)

func vaultStorage(ctx context.Context) error {
	vaultClient, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		return errors.Errorf("create Vault client: %w", err)
	}
	storage := vault.NewSessionStorage(vaultClient, "cubbyhole/telegram/user", "session")

	client, err := telegram.ClientFromEnvironment(telegram.Options{
		SessionStorage: storage,
	})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		_, err := client.Auth().Bot(ctx, os.Getenv("BOT_TOKEN"))
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := vaultStorage(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}
Output:

func NewSessionStorage

func NewSessionStorage(client *api.Client, path, key string) SessionStorage

NewSessionStorage creates new SessionStorage.

Jump to

Keyboard shortcuts

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