client

package module
v0.0.0-...-12da56e Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: MIT Imports: 22 Imported by: 0

README

HTTP Client for keys.pub

This documentation is in progress.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets logger for the package.

Types

type Client

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

Client ...

func New

func New(urs string) (*Client, error)

New creates a Client for an HTTP API.

func (*Client) AdminCheck

func (c *Client) AdminCheck(ctx context.Context, kid keys.ID, admin *keys.EdX25519Key) error

AdminCheck performs user & sigchain associated with key by an admin. The server periodically checks users and sigchains, but this tells the server to do it right away.

func (*Client) Check

func (c *Client) Check(ctx context.Context, key *keys.EdX25519Key) error

Check user & sigchain associated with edx25519 key. The server periodically checks users and sigchains, but this tells the server to do it right away.

func (*Client) CreateInvite

func (c *Client) CreateInvite(ctx context.Context, sender *keys.EdX25519Key, recipient keys.ID) (*api.CreateInviteResponse, error)

CreateInvite writes a sender recipient address (invite).

func (*Client) DecryptMessage

func (c *Client) DecryptMessage(key *keys.EdX25519Key, msg *Message) ([]byte, keys.ID, error)

DecryptMessage decrypts a message from Messages.

func (*Client) DeleteDisco

func (c *Client) DeleteDisco(ctx context.Context, sender *keys.EdX25519Key, recipient keys.ID) error

DeleteDisco removes discovery addresses.

func (*Client) GetDisco

func (c *Client) GetDisco(ctx context.Context, sender keys.ID, recipient *keys.EdX25519Key, typ DiscoType) (string, error)

GetDisco gets a discovery address.

func (*Client) Invite

func (c *Client) Invite(ctx context.Context, sender *keys.EdX25519Key, code string) (*api.InviteResponse, error)

Invite looks for an invite with code.

func (*Client) Messages

func (c *Client) Messages(ctx context.Context, key *keys.EdX25519Key, from keys.ID, opts *MessagesOpts) ([]*Message, string, error)

Messages returns encrypted messages. To decrypt a message, use Client#DecryptMessage.

func (*Client) Now

func (c *Client) Now() time.Time

Now returns current time.

func (*Client) PutDisco

func (c *Client) PutDisco(ctx context.Context, sender *keys.EdX25519Key, recipient keys.ID, typ DiscoType, data string, expire time.Duration) error

PutDisco puts a discovery offer or answer.

func (*Client) PutSigchainStatement

func (c *Client) PutSigchainStatement(ctx context.Context, st *keys.Statement) error

PutSigchainStatement ...

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, sender *keys.EdX25519Key, recipient keys.ID, b []byte, expire time.Duration) (*api.CreateMessageResponse, error)

SendMessage posts an encrypted expiring message.

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(httpClient *http.Client)

SetHTTPClient sets the http.Client to use.

func (*Client) SetTimeNow

func (c *Client) SetTimeNow(nowFn func() time.Time)

SetTimeNow sets the clock Now fn.

func (*Client) Sigchain

func (c *Client) Sigchain(ctx context.Context, kid keys.ID) (*api.SigchainResponse, error)

Sigchain for KID. If sigchain not found, a nil response is returned.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/keys-pub/keys"
	"github.com/keys-pub/keys/request"
	"github.com/keys-pub/keys/user"
	"github.com/keys-pub/keysd/http/client"
)

func main() {
	cl, err := client.New("https://keys.pub")
	if err != nil {
		log.Fatal(err)
	}

	// Get the keys.Sigchain.
	kid := keys.ID("kex1ydecaulsg5qty2axyy770cjdvqn3ef2qa85xw87p09ydlvs5lurq53x0p3")
	resp, err := cl.Sigchain(context.TODO(), kid)
	if err != nil {
		log.Fatal(err)
	}
	sc, err := resp.Sigchain()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", sc.Spew())

	// Find the user.User in the keys.Sigchain.
	usr, err := user.FindInSigchain(sc)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", usr)

	// Request the User URL and verify it.
	result := user.RequestVerify(context.TODO(), request.NewHTTPRequestor(), usr, time.Now())
	if result.Status != user.StatusOK {
		log.Fatalf("User check failed: %+v", result)
	}

	// Check result status.
	fmt.Printf("%s\n", result.Status)

}
Output:

/sigchain/kex1ydecaulsg5qty2axyy770cjdvqn3ef2qa85xw87p09ydlvs5lurq53x0p3/1 {".sig":"Rf39uRlHXTqmOjcqUs5BTssshRdqpaFfmoYYJuA5rNVGgGd/bRG8p8ZebB8K+w9kozMpnuAoa4lko+oPHcabCQ==","data":"eyJrIjoia2V4MXlkZWNhdWxzZzVxdHkyYXh5eTc3MGNqZHZxbjNlZjJxYTg1eHc4N3AwOXlkbHZzNWx1cnE1M3gwcDMiLCJuIjoia2V5cy5wdWIiLCJzcSI6MSwic3IiOiJodHRwcyIsInUiOiJodHRwczovL2tleXMucHViL2tleXNwdWIudHh0In0=","kid":"kex1ydecaulsg5qty2axyy770cjdvqn3ef2qa85xw87p09ydlvs5lurq53x0p3","seq":1,"ts":1588276919715,"type":"user"}
keys.pub@https!kex1ydecaulsg5qty2axyy770cjdvqn3ef2qa85xw87p09ydlvs5lurq53x0p3-1#https://keys.pub/keyspub.txt
ok

func (*Client) URL

func (c *Client) URL() *url.URL

URL ...

func (*Client) User

func (c *Client) User(ctx context.Context, kid keys.ID) (*api.UserResponse, error)

User ...

func (*Client) UserSearch

func (c *Client) UserSearch(ctx context.Context, query string, limit int) (*api.UserSearchResponse, error)

UserSearch ...

type ContextLogger

type ContextLogger interface {
	Debugf(ctx context.Context, format string, args ...interface{})
	Infof(ctx context.Context, format string, args ...interface{})
	Warningf(ctx context.Context, format string, args ...interface{})
	Errorf(ctx context.Context, format string, args ...interface{})
}

ContextLogger interface used in this package with request context.

func NewContextLogger

func NewContextLogger(lev LogLevel) ContextLogger

NewContextLogger ...

type DiscoType

type DiscoType string

DiscoType is the type of discovery address.

const (
	// Offer initiates.
	Offer DiscoType = "offer"
	// Answer listens.
	Answer DiscoType = "answer"
)

type ErrResponse

type ErrResponse struct {
	StatusCode int
	Message    string
	URL        *url.URL
}

ErrResponse ...

func (ErrResponse) Error

func (e ErrResponse) Error() string

type LogLevel

type LogLevel int

LogLevel ...

const (
	// DebugLevel ...
	DebugLevel LogLevel = 3
	// InfoLevel ...
	InfoLevel LogLevel = 2
	// WarnLevel ...
	WarnLevel LogLevel = 1
	// ErrLevel ...
	ErrLevel LogLevel = 0
)

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger interface used in this package.

func NewLogger

func NewLogger(lev LogLevel) Logger

NewLogger ...

type Message

type Message struct {
	ID   string
	Data []byte

	CreatedAt time.Time
	UpdatedAt time.Time
}

Message from server.

type MessagesOpts

type MessagesOpts struct {
	// Version to list to/from
	Version string
	// Direction ascending or descending
	Direction ds.Direction
	// Limit by
	Limit int
}

MessagesOpts options for Messages.

Jump to

Keyboard shortcuts

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