~chipaca/ubuntu-push/whoopsie-whoopsie-whoopsie

« back to all changes in this revision

Viewing changes to client/session/session.go

  • Committer: Tarmac
  • Author(s): Diogo Baeder de Paula Pinto
  • Date: 2014-04-17 15:52:09 UTC
  • mfrom: (124.2.7 auth)
  • Revision ID: tarmac-20140417155209-yzxm94t4cur0aq2t
[r=chipaca] Moving the authorization retrieval to the client session start, so that it can be retried at a later time

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
        "launchpad.net/ubuntu-push/util"
39
39
)
40
40
 
41
 
var wireVersionBytes = []byte{protocol.ProtocolWireVersion}
 
41
var (
 
42
        wireVersionBytes = []byte{protocol.ProtocolWireVersion}
 
43
        getAuthorization = util.GetAuthorization
 
44
        shouldGetAuth    = false
 
45
)
42
46
 
43
47
type Notification struct {
44
48
        TopLevel int64
84
88
        ExpectAllRepairedTime  time.Duration
85
89
        PEM                    []byte
86
90
        Info                   map[string]interface{}
87
 
        Authorization          string
88
91
}
89
92
 
90
93
// ClientSession holds a client<->server session and its configuration.
145
148
                TLS:                 &tls.Config{},
146
149
                stateP:              &state,
147
150
                timeSince:           time.Since,
148
 
                auth:                conf.Authorization,
149
151
        }
150
152
        if sess.PEM != nil {
151
153
                cp := x509.NewCertPool()
201
203
        return nil
202
204
}
203
205
 
 
206
// checkAuthorization checks the authorization within the phone
 
207
func (sess *ClientSession) checkAuthorization() error {
 
208
        // grab the authorization string from the accounts
 
209
        // TODO: remove this condition when we have a way to deal with failing authorizations
 
210
        if shouldGetAuth {
 
211
                auth, err := getAuthorization()
 
212
                if err != nil {
 
213
                        // For now we just log the error, as we don't want to block unauthorized users
 
214
                        sess.Log.Errorf("unable to get the authorization token from the account: %v", err)
 
215
                }
 
216
                sess.auth = auth
 
217
        }
 
218
        return nil
 
219
}
 
220
 
204
221
func (sess *ClientSession) resetHosts() {
205
222
        sess.deliveryHosts = nil
206
223
}
453
470
 
454
471
// run calls connect, and if it works it calls start, and if it works
455
472
// it runs loop in a goroutine, and ships its return value over ErrCh.
456
 
func (sess *ClientSession) run(closer func(), hostGetter, connecter, starter, looper func() error) error {
 
473
func (sess *ClientSession) run(closer func(), authChecker, hostGetter, connecter, starter, looper func() error) error {
457
474
        closer()
458
 
        err := hostGetter()
459
 
        if err != nil {
460
 
                return err
461
 
        }
462
 
        err = connecter()
 
475
        if err := authChecker(); err != nil {
 
476
                return err
 
477
        }
 
478
        if err := hostGetter(); err != nil {
 
479
                return err
 
480
        }
 
481
        err := connecter()
463
482
        if err == nil {
464
483
                err = starter()
465
484
                if err == nil {
489
508
                // keep on trying.
490
509
                panic("can't Dial() without a protocol constructor.")
491
510
        }
492
 
        return sess.run(sess.doClose, sess.getHosts, sess.connect, sess.start, sess.loop)
 
511
        return sess.run(sess.doClose, sess.checkAuthorization, sess.getHosts, sess.connect, sess.start, sess.loop)
493
512
}
494
513
 
495
514
func init() {