auth

Paddy 2015-05-17 Parent:37a42585660e

170:bdb83e88e1da Go to Latest

auth/profile_verification.go

Instantiate a login verification handler. When starting up the authd binary, get our login verification handler set up. We're going to use environment variables to detect if NSQ is configured. If it is, that'll be used. Otherwise, fall back on logging to stdout.

History
1 package auth
3 import (
4 "log"
5 "time"
7 "code.secondbit.org/events.hg"
8 )
10 const (
11 EventSystem = "authd"
12 EventModelLogin = "login"
13 EventActionSendVerification = "send_verification"
14 EventTopicLoginVerification = "login_verification"
15 )
17 type loginVerificationNotifier interface {
18 SendLoginVerification(login Login)
19 }
21 type stdoutNotifier struct{}
23 func NewStdoutNotifier() stdoutNotifier {
24 return stdoutNotifier{}
25 }
27 func (s stdoutNotifier) SendLoginVerification(login Login) {
28 log.Printf("Use \"%s\" as the verification code for \"%s\"\n", login.Verification, login.Value)
29 }
31 type nsqNotifier struct {
32 *events.NSQPublisher
33 }
35 func NewNSQNotifier(address string) (*nsqNotifier, error) {
36 p, err := events.NewNSQPublisher(EventSystem+"/"+Version, address)
37 return &nsqNotifier{p}, err
38 }
40 func (n *nsqNotifier) SendLoginVerification(login Login) {
41 evt := events.Event{
42 System: EventSystem,
43 Model: EventModelLogin,
44 ID: login.Value,
45 Action: EventActionSendVerification,
46 Timestamp: time.Now(),
47 Data: map[string]string{
48 "verification": login.Verification,
49 },
50 }
51 err := n.Publish(EventTopicLoginVerification, evt)
52 if err != nil {
53 log.Printf("Error sending event: %#+v\n", err)
54 }
55 }