1package client
2
3import (
4	"reflect"
5)
6
7// FieldRegistry is designed to look and feel
8// like an enum from another language like Python.
9//
10// Example: Accessing constants
11//
12//   FieldRegistry.AccountExpires
13//   FieldRegistry.BadPasswordCount
14//
15// Example: Utility methods
16//
17//   FieldRegistry.List()
18//   FieldRegistry.Parse("givenName")
19//
20var FieldRegistry = newFieldRegistry()
21
22// newFieldRegistry iterates through all the fields in the registry,
23// pulls their ldap strings, and sets up each field to contain its ldap string
24func newFieldRegistry() *fieldRegistry {
25	reg := &fieldRegistry{}
26	vOfReg := reflect.ValueOf(reg)
27
28	registryFields := vOfReg.Elem()
29	for i := 0; i < registryFields.NumField(); i++ {
30
31		if registryFields.Field(i).Kind() == reflect.Ptr {
32
33			field := registryFields.Type().Field(i)
34			ldapString := field.Tag.Get("ldap")
35			ldapField := &Field{ldapString}
36			vOfLDAPField := reflect.ValueOf(ldapField)
37
38			registryFields.FieldByName(field.Name).Set(vOfLDAPField)
39
40			reg.fieldList = append(reg.fieldList, ldapField)
41		}
42	}
43	return reg
44}
45
46// fieldRegistry isn't currently intended to be an exhaustive list -
47// there are more fields in ActiveDirectory. However, these are the ones
48// that may be useful to Vault. Feel free to add to this list!
49type fieldRegistry struct {
50	AccountExpires              *Field `ldap:"accountExpires"`
51	AdminCount                  *Field `ldap:"adminCount"`
52	BadPasswordCount            *Field `ldap:"badPwdCount"`
53	BadPasswordTime             *Field `ldap:"badPasswordTime"`
54	CodePage                    *Field `ldap:"codePage"`
55	CommonName                  *Field `ldap:"cn"`
56	CountryCode                 *Field `ldap:"countryCode"`
57	DisplayName                 *Field `ldap:"displayName"`
58	DistinguishedName           *Field `ldap:"distinguishedName"`
59	DomainComponent             *Field `ldap:"dc"`
60	DomainName                  *Field `ldap:"dn"`
61	DSCorePropogationData       *Field `ldap:"dSCorePropagationData"`
62	GivenName                   *Field `ldap:"givenName"`
63	GroupType                   *Field `ldap:"groupType"`
64	Initials                    *Field `ldap:"initials"`
65	InstanceType                *Field `ldap:"instanceType"`
66	LastLogoff                  *Field `ldap:"lastLogoff"`
67	LastLogon                   *Field `ldap:"lastLogon"`
68	LastLogonTimestamp          *Field `ldap:"lastLogonTimestamp"`
69	LockoutTime                 *Field `ldap:"lockoutTime"`
70	LogonCount                  *Field `ldap:"logonCount"`
71	MemberOf                    *Field `ldap:"memberOf"`
72	Name                        *Field `ldap:"name"`
73	ObjectCategory              *Field `ldap:"objectCategory"`
74	ObjectClass                 *Field `ldap:"objectClass"`
75	ObjectGUID                  *Field `ldap:"objectGUID"`
76	ObjectSID                   *Field `ldap:"objectSid"`
77	OrganizationalUnit          *Field `ldap:"ou"`
78	PasswordLastSet             *Field `ldap:"pwdLastSet"`
79	PrimaryGroupID              *Field `ldap:"primaryGroupID"`
80	SAMAccountName              *Field `ldap:"sAMAccountName"`
81	SAMAccountType              *Field `ldap:"sAMAccountType"`
82	Surname                     *Field `ldap:"sn"`
83	UnicodePassword             *Field `ldap:"unicodePwd"`
84	UpdateSequenceNumberChanged *Field `ldap:"uSNChanged"`
85	UpdateSequenceNumberCreated *Field `ldap:"uSNCreated"`
86	UserAccountControl          *Field `ldap:"userAccountControl"`
87	UserPrincipalName           *Field `ldap:"userPrincipalName"`
88	WhenCreated                 *Field `ldap:"whenCreated"`
89	WhenChanged                 *Field `ldap:"whenChanged"`
90
91	fieldList []*Field
92}
93
94func (r *fieldRegistry) List() []*Field {
95	return r.fieldList
96}
97
98func (r *fieldRegistry) Parse(s string) *Field {
99	for _, f := range r.List() {
100		if f.String() == s {
101			return f
102		}
103	}
104	return nil
105}
106
107type Field struct {
108	str string
109}
110
111func (f *Field) String() string {
112	return f.str
113}
114