shared.go

 1// Copyright (c) Microsoft Corporation.
 2// Licensed under the MIT license.
 3
 4package shared
 5
 6import (
 7	"net/http"
 8	"reflect"
 9	"strings"
10)
11
12const (
13	// CacheKeySeparator is used in creating the keys of the cache.
14	CacheKeySeparator = "-"
15)
16
17type Account struct {
18	HomeAccountID     string `json:"home_account_id,omitempty"`
19	Environment       string `json:"environment,omitempty"`
20	Realm             string `json:"realm,omitempty"`
21	LocalAccountID    string `json:"local_account_id,omitempty"`
22	AuthorityType     string `json:"authority_type,omitempty"`
23	PreferredUsername string `json:"username,omitempty"`
24	GivenName         string `json:"given_name,omitempty"`
25	FamilyName        string `json:"family_name,omitempty"`
26	MiddleName        string `json:"middle_name,omitempty"`
27	Name              string `json:"name,omitempty"`
28	AlternativeID     string `json:"alternative_account_id,omitempty"`
29	RawClientInfo     string `json:"client_info,omitempty"`
30	UserAssertionHash string `json:"user_assertion_hash,omitempty"`
31
32	AdditionalFields map[string]interface{}
33}
34
35// NewAccount creates an account.
36func NewAccount(homeAccountID, env, realm, localAccountID, authorityType, username string) Account {
37	return Account{
38		HomeAccountID:     homeAccountID,
39		Environment:       env,
40		Realm:             realm,
41		LocalAccountID:    localAccountID,
42		AuthorityType:     authorityType,
43		PreferredUsername: username,
44	}
45}
46
47// Key creates the key for storing accounts in the cache.
48func (acc Account) Key() string {
49	key := strings.Join([]string{acc.HomeAccountID, acc.Environment, acc.Realm}, CacheKeySeparator)
50	return strings.ToLower(key)
51}
52
53// IsZero checks the zero value of account.
54func (acc Account) IsZero() bool {
55	v := reflect.ValueOf(acc)
56	for i := 0; i < v.NumField(); i++ {
57		field := v.Field(i)
58		if !field.IsZero() {
59			switch field.Kind() {
60			case reflect.Map, reflect.Slice:
61				if field.Len() == 0 {
62					continue
63				}
64			}
65			return false
66		}
67	}
68	return true
69}
70
71// DefaultClient is our default shared HTTP client.
72var DefaultClient = &http.Client{}