Skip to content

hazcod/go-intigriti

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-intigriti

Go library and commandline client for interacting with the Intigriti external API.

Checkout the autogenerated SDK docs on pkg.go.dev.

Commandline client

Usage:

# list out all company programs
# also try: inti c list
% inti company list-programs

# list out all company submissions across all programs
# also try: inti c sub
% inti company list-submissions

# verify if a specific IP address is linked to an Intigriti user
# also try: inti c ip 1.1.1.1
% inti company check-ip 1.1.1.1

Setup

Ensure the external API enabled on your company account and an integration is created with a redirect URI value of http://localhost:1337/. Afterwards create the following local configuration file:

log.level: info
auth:
    client_id: YOUR-CLIENT-ID
    client_secret: YOUR-CLIENT-SECRET

For the first call it will ask you to perform browser interaction to authenticate.
Future calls will not need to since your token will be cached in your configuration file.

If you selected 'non-expiring access tokens in the Intigriti administration panel, this code will only need interactive authentication once.
Afterwards, it will re-use the access token in your YAML configuration file.

Library

API Swagger documentation is available on the ReadMe.

Usage

package main

import (
	"flag"
	"github.com/hazcod/go-intigriti/cmd/cli/company"
	"github.com/hazcod/go-intigriti/cmd/config"
	intigriti "github.com/hazcod/go-intigriti/pkg/api"
	apiConfig "github.com/hazcod/go-intigriti/pkg/config"
	"github.com/sirupsen/logrus"
	"strings"
)

func main() {
	logger := logrus.New()

	configPath := flag.String("config", "inti.yml", "Path to your config file.")
	logLevelStr := flag.String("log", "", "Log level.")
	flag.Parse()

	if *logLevelStr != "" {
		logLevel, err := logrus.ParseLevel(*logLevelStr)
		if err != nil {
			logger.WithError(err).Fatal("could not parse log level")
		}

		logger.SetLevel(logLevel)
		logger.WithField("level", logLevel.String()).Debugf("log level set")
	}

	cfg, err := config.Load(logger, *configPath)
	if err != nil {
		logger.Fatalf("could not load configuration: %s", err)
	}

	if err := cfg.Validate(); err != nil {
		logger.WithError(err).Fatal("invalid configuration")
	}

	if cfg.Log.Level != "" && *logLevelStr == "" {
		logLevel, err := logrus.ParseLevel(cfg.Log.Level)
		if err != nil {
			logger.WithError(err).Fatal("could not parse log level")
		}

		logger.SetLevel(logLevel)
		logger.WithField("level", logLevel.String()).Debugf("log level set")
	}

	//browser := ui.SystemBrowser{}
	apiScopes := []string{"company_external_api", "core_platform:read"}

	inti, err := intigriti.New(apiConfig.Config{
		// our Intigriti API credentials
		Credentials: struct {
			ClientID     string
			ClientSecret string
		}{ClientID: cfg.Auth.ClientID, ClientSecret: cfg.Auth.ClientSecret},
		APIScopes: apiScopes,

		// cache tokens as much as possible to reduce times we have to authenticate
		TokenCache: &apiConfig.CachedToken{
			RefreshToken: cfg.Cache.RefreshToken,
			AccessToken:  cfg.Cache.AccessToken,
			ExpiryDate:   cfg.Cache.ExpiryDate,
			Type:         cfg.Cache.Type,
		},

		// use our logger and our logging levels
		Logger: logger,
	})
	if err != nil {
		logger.WithError(err).Fatal("could not initialize client")
	}

	token, err := inti.GetToken()
	if err != nil {
		logger.Fatalf("failed to cache token: %v", err)
	}

	if err := cfg.CacheAuth(logger, *configPath, token); err != nil {
		logger.Fatalf("failed to cache token: %v", err)
	}

	logger.WithField("authenticated", inti.IsAuthenticated()).Debug("initialized client")

	if err != nil {
		logger.Fatal(err)
	}

	programs, err := inti.GetPrograms()
	if err != nil {
		logger.Fatal(err)
	}

	for _, program := range programs {
		logger.Println(program.Name)
	}
}

Testing

# test on production using inti.yml
go test -tags integration -v ./...

# test on staging using inti.yml
INTI_TOKEN_URL=="testing.api.com" INTI_AUTH_URL=="subs.testing.api.com" INTI_API_URL="api.testing.com" go test -tags integration -v ./...