client

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package client contains clients for the CYBERCRYPT D1 Generic service.

Example
// Code generated by copy-client.sh. DO NOT EDIT.
// version: v2.0.0
// source: https://github.com/cybercryptio/d1-service-generic.git
// commit: 05a3b04a1916d3e6c1bfea65041b7536cf93778e

//go:build !skipexamples
// +build !skipexamples

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	pbauthn "github.com/cybercryptio/d1-client-go/v2/d1-generic/protobuf/authn"
	pbgeneric "github.com/cybercryptio/d1-client-go/v2/d1-generic/protobuf/generic"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/metadata"
)

var endpoint = os.Getenv("D1_ENDPOINT")
var uid = os.Getenv("D1_UID")
var password = os.Getenv("D1_PASS")
var creds = insecure.NewCredentials()

func main() {
	// Create a new D1 Generic client providing the hostname, and optionally, the client connection level credentials.
	client, err := NewGenericClient(endpoint, WithGrpcOption(grpc.WithTransportCredentials(creds)))
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()

	// Login the user with their credentials.
	loginResponse, err := client.Authn.LoginUser(
		ctx,
		&pbauthn.LoginUserRequest{
			UserId:   uid,
			Password: password,
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	// Set access token for future calls.
	ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "bearer "+loginResponse.AccessToken)

	// Encrypt sensitive data.
	encryptResponse, err := client.Generic.Encrypt(
		ctx,
		&pbgeneric.EncryptRequest{
			Plaintext:      []byte("secret data"),
			AssociatedData: []byte("metadata"),
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	// Decrypt the response.
	decryptResponse, err := client.Generic.Decrypt(
		ctx,
		&pbgeneric.DecryptRequest{
			ObjectId:       encryptResponse.ObjectId,
			Ciphertext:     encryptResponse.Ciphertext,
			AssociatedData: encryptResponse.AssociatedData,
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("plaintext:%q associated_data:%q",
		decryptResponse.Plaintext,
		decryptResponse.AssociatedData,
	)
}
Output:

plaintext:"secret data" associated_data:"metadata"
Example (WithPerRPCCredentials)
// Create a new D1 Generic client providing the hostname, and optionally, the client connection level and per RPC credentials.
client, err := NewGenericClient(endpoint,
	WithGrpcOption(grpc.WithTransportCredentials(creds)),
	WithTokenRefresh(uid, password),
)
if err != nil {
	log.Fatal(err)
}

ctx := context.Background()

// Encrypt sensitive data.
encryptResponse, err := client.Generic.Encrypt(
	ctx,
	&pbgeneric.EncryptRequest{
		Plaintext:      []byte("secret data"),
		AssociatedData: []byte("metadata"),
	},
)
if err != nil {
	log.Fatal(err)
}

// Decrypt the response.
decryptResponse, err := client.Generic.Decrypt(
	ctx,
	&pbgeneric.DecryptRequest{
		ObjectId:       encryptResponse.ObjectId,
		Ciphertext:     encryptResponse.Ciphertext,
		AssociatedData: encryptResponse.AssociatedData,
	},
)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("plaintext:%q associated_data:%q",
	decryptResponse.Plaintext,
	decryptResponse.AssociatedData,
)
Output:

plaintext:"secret data" associated_data:"metadata"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseClient

type BaseClient struct {
	Version    pbversion.VersionClient
	Authn      pbauthn.AuthnClient
	Authz      pbauthz.AuthzClient
	Health     grpc_health_v1.HealthClient
	Index      pbindex.IndexClient
	Connection *grpc.ClientConn
}

BaseClient represents the shared functionality between various D1 services.

func NewBaseClient

func NewBaseClient(endpoint string, opts ...Option) (BaseClient, error)

NewBaseClient creates a new client for the given endpoint, configured with the provided options.

func (*BaseClient) Close

func (b *BaseClient) Close() error

Close closes all connections to the server.

type GenericClient

type GenericClient struct {
	BaseClient
	Generic pb.GenericClient
}

GenericClient can be used to make calls to a D1 Generic service.

func NewGenericClient

func NewGenericClient(endpoint string, opts ...Option) (GenericClient, error)

NewGenericClient creates a new client for the given endpoint, configured with the provided options.

type Option

type Option func(*BaseClient) grpc.DialOption

Option is used configure optional settings on the client.

func WithGrpcOption

func WithGrpcOption(option grpc.DialOption) Option

WithGrpcOption returns an Option which configures the underlying gRPC connection.

func WithTokenRefresh

func WithTokenRefresh(uid, pwd string) Option

WithTokenRefresh returns an Option that configures token refresh.

Directories

Path Synopsis
protobuf

Jump to

Keyboard shortcuts

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