go-spectre/spectre.go

78 lines
1.4 KiB
Go

package spectre
import "encoding/binary"
// Spectre is a spectre client
type Spectre struct {
name string
secret string
scoper Scoper
key []byte
}
// New returns a Spectre client
func New(name, secret string, opts ...Option) (s *Spectre, err error) {
s = &Spectre{
name: name,
secret: secret,
scoper: DefaultScoper,
}
for _, opt := range opts {
opt(s)
}
s.key, err = userKey(s.name, s.secret, s.scoper)
return
}
// Option is a Spectre option
type Option func(*Spectre)
// WithScoper assigns a scoper to Spectre
func WithScoper(scoper Scoper) Option {
return func(s *Spectre) {
s.scoper = scoper
}
}
// Site returns a site password based on Options
func (s *Spectre) Site(siteName string, opts ...SiteOption) string {
return site(s.key, s.scoper, siteName, opts...)
}
type options struct {
template Template
counter int
scope Scope
}
// SiteOption is an option for Spectre.Site
type SiteOption func(*options)
// WithTemplate specifies a Template
func WithTemplate(t Template) SiteOption {
return func(opts *options) {
opts.template = t
}
}
// WithCounter specifies a counter
func WithCounter(c int) SiteOption {
return func(opts *options) {
opts.counter = c
}
}
// WithScope specifies a Scope
func WithScope(s Scope) SiteOption {
return func(opts *options) {
opts.scope = s
}
}
func bigEndian(num int) []byte {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(num))
return buf
}