blob: eda20812ea2155e2b127f347794266b7203d7775 [file] [log] [blame]
Asim Shankar61071792014-07-22 13:03:18 -07001package blesser
2
3import (
4 "time"
5
6 "veyron/services/identity"
7 "veyron/services/identity/googleoauth"
8 "veyron2"
9 "veyron2/ipc"
10 "veyron2/vdl/vdlutil"
11)
12
13// Expiry time of blessings issued by the Google OAuth Blesser Server.
14// TODO(ashankar): This is ridiculously large! Add third-party revocation
15// caveat instead?
16const BlessingExpiry = 365 * 24 * time.Hour
17
18type googleOAuth struct {
19 rt veyron2.Runtime
20 clientID, clientSecret string
21}
22
23// NewGoogleOAuthBlesserServer provides an identity.OAuthBlesserService that uses authorization
24// codes to obtain the username of a client and provide blessings with that name.
25//
26// For more details, see documentation on Google OAuth 2.0 flows:
27// https://developers.google.com/accounts/docs/OAuth2
28func NewGoogleOAuthBlesserServer(rt veyron2.Runtime, clientID, clientSecret string) interface{} {
29 return identity.NewServerOAuthBlesser(&googleOAuth{rt, clientID, clientSecret})
30}
31
32func (b *googleOAuth) Bless(ctx ipc.ServerContext, authcode, redirectURL string) (vdlutil.Any, error) {
33 config := googleoauth.NewOAuthConfig(b.clientID, b.clientSecret, redirectURL)
34 name, err := googleoauth.ExchangeAuthCodeForEmail(config, authcode)
35 if err != nil {
36 return nil, err
37 }
38 self := b.rt.Identity()
39 // Use the blessing that was used to authenticate with the client to bless it.
40 if self, err = self.Derive(ctx.LocalID()); err != nil {
41 return nil, err
42 }
43 // TODO(ashankar,ataly): Use the same set of caveats as is used by the HTTP handler.
44 // For example, a third-party revocation caveat?
45 // TODO(ashankar,rthellend): Also want the domain restriction here?
46 return self.Bless(ctx.RemoteID(), name, BlessingExpiry, nil)
47}