Asim Shankar | 6107179 | 2014-07-22 13:03:18 -0700 | [diff] [blame] | 1 | package blesser |
| 2 | |
| 3 | import ( |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 4 | "encoding/json" |
Asim Shankar | 7106157 | 2014-07-22 16:59:18 -0700 | [diff] [blame] | 5 | "fmt" |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 6 | "net/http" |
Asim Shankar | 6107179 | 2014-07-22 13:03:18 -0700 | [diff] [blame] | 7 | "time" |
| 8 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 9 | "veyron.io/veyron/veyron/services/identity" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 10 | "veyron.io/veyron/veyron/services/identity/revocation" |
Suharsh Sivakumar | fb5cbb7 | 2014-08-27 13:14:22 -0700 | [diff] [blame] | 11 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron2/ipc" |
| 13 | "veyron.io/veyron/veyron2/security" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 14 | "veyron.io/veyron/veyron2/vlog" |
Asim Shankar | 6107179 | 2014-07-22 13:03:18 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
Asim Shankar | 6107179 | 2014-07-22 13:03:18 -0700 | [diff] [blame] | 17 | type googleOAuth struct { |
Suharsh Sivakumar | fb5cbb7 | 2014-08-27 13:14:22 -0700 | [diff] [blame] | 18 | authcodeClient struct{ ID, Secret string } |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 19 | accessTokenClients []AccessTokenClient |
Suharsh Sivakumar | fb5cbb7 | 2014-08-27 13:14:22 -0700 | [diff] [blame] | 20 | duration time.Duration |
| 21 | domain string |
| 22 | dischargerLocation string |
| 23 | revocationManager *revocation.RevocationManager |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 26 | // AccessTokenClient represents a client of the BlessUsingAccessToken RPCs. |
| 27 | type AccessTokenClient struct { |
| 28 | // Descriptive name of the client. |
| 29 | Name string |
| 30 | // OAuth Client ID. |
| 31 | ClientID string |
| 32 | } |
| 33 | |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 34 | // GoogleParams represents all the parameters provided to NewGoogleOAuthBlesserServer |
| 35 | type GoogleParams struct { |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 36 | // The OAuth client IDs and names for the clients of the BlessUsingAccessToken RPCs. |
| 37 | AccessTokenClients []AccessTokenClient |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 38 | // If non-empty, only email addresses from this domain will be blessed. |
| 39 | DomainRestriction string |
Suharsh Sivakumar | fb5cbb7 | 2014-08-27 13:14:22 -0700 | [diff] [blame] | 40 | // The object name of the discharger service. If this is empty then revocation caveats will not be granted. |
| 41 | DischargerLocation string |
| 42 | // The revocation manager that generates caveats and manages revocation. |
| 43 | RevocationManager *revocation.RevocationManager |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 44 | // The duration for which blessings will be valid. (Used iff RevocationManager is nil). |
| 45 | BlessingDuration time.Duration |
Asim Shankar | 6107179 | 2014-07-22 13:03:18 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // NewGoogleOAuthBlesserServer provides an identity.OAuthBlesserService that uses authorization |
| 49 | // codes to obtain the username of a client and provide blessings with that name. |
| 50 | // |
| 51 | // For more details, see documentation on Google OAuth 2.0 flows: |
| 52 | // https://developers.google.com/accounts/docs/OAuth2 |
Asim Shankar | 7106157 | 2014-07-22 16:59:18 -0700 | [diff] [blame] | 53 | // |
| 54 | // Blessings generated by this server expire after duration. If domain is non-empty, then blessings |
| 55 | // are generated only for email addresses from that domain. |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 56 | func NewGoogleOAuthBlesserServer(p GoogleParams) interface{} { |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 57 | return identity.OAuthBlesserServer(&googleOAuth{ |
Suharsh Sivakumar | fb5cbb7 | 2014-08-27 13:14:22 -0700 | [diff] [blame] | 58 | duration: p.BlessingDuration, |
| 59 | domain: p.DomainRestriction, |
| 60 | dischargerLocation: p.DischargerLocation, |
| 61 | revocationManager: p.RevocationManager, |
Suharsh Sivakumar | d308c7e | 2014-10-03 12:46:50 -0700 | [diff] [blame] | 62 | accessTokenClients: p.AccessTokenClients, |
| 63 | }) |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 66 | func (b *googleOAuth) BlessUsingAccessToken(ctx ipc.ServerContext, accesstoken string) (security.WireBlessings, string, error) { |
| 67 | var noblessings security.WireBlessings |
Srdjan Petrovic | a1e6ddb | 2014-09-08 10:18:44 -0700 | [diff] [blame] | 68 | if len(b.accessTokenClients) == 0 { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 69 | return noblessings, "", fmt.Errorf("server not configured for blessing based on access tokens") |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 70 | } |
| 71 | // URL from: https://developers.google.com/accounts/docs/OAuth2UserAgent#validatetoken |
| 72 | tokeninfo, err := http.Get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + accesstoken) |
| 73 | if err != nil { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 74 | return noblessings, "", fmt.Errorf("unable to use token: %v", err) |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 75 | } |
| 76 | if tokeninfo.StatusCode != http.StatusOK { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 77 | return noblessings, "", fmt.Errorf("unable to verify access token: %v", tokeninfo.StatusCode) |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 78 | } |
| 79 | // tokeninfo contains a JSON-encoded struct |
| 80 | var token struct { |
| 81 | IssuedTo string `json:"issued_to"` |
| 82 | Audience string `json:"audience"` |
| 83 | UserID string `json:"user_id"` |
| 84 | Scope string `json:"scope"` |
| 85 | ExpiresIn int64 `json:"expires_in"` |
| 86 | Email string `json:"email"` |
| 87 | VerifiedEmail bool `json:"verified_email"` |
| 88 | AccessType string `json:"access_type"` |
| 89 | } |
| 90 | if err := json.NewDecoder(tokeninfo.Body).Decode(&token); err != nil { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 91 | return noblessings, "", fmt.Errorf("invalid JSON response from Google's tokeninfo API: %v", err) |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 92 | } |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 93 | var client AccessTokenClient |
Srdjan Petrovic | a1e6ddb | 2014-09-08 10:18:44 -0700 | [diff] [blame] | 94 | audienceMatch := false |
| 95 | for _, c := range b.accessTokenClients { |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 96 | if token.Audience == c.ClientID { |
| 97 | client = c |
Srdjan Petrovic | a1e6ddb | 2014-09-08 10:18:44 -0700 | [diff] [blame] | 98 | audienceMatch = true |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | if !audienceMatch { |
| 103 | vlog.Infof("Got access token [%+v], wanted one of client ids %v", token, b.accessTokenClients) |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 104 | return noblessings, "", fmt.Errorf("token not meant for this purpose, confused deputy? https://developers.google.com/accounts/docs/OAuth2UserAgent#validatetoken") |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 105 | } |
| 106 | if !token.VerifiedEmail { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 107 | return noblessings, "", fmt.Errorf("email not verified") |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 108 | } |
Srdjan Petrovic | 89779ad | 2014-11-03 15:40:36 -0800 | [diff] [blame] | 109 | // Append client.Name to the blessing (e.g., "android", "chrome"). Since blessings issued by |
| 110 | // this process do not have many caveats on them and typically have a large expiry duration, |
| 111 | // we append this suffix so that servers can explicitly distinguish these clients while |
| 112 | // specifying authorization policies (say, via ACLs). |
| 113 | return b.bless(ctx, token.Email+security.ChainSeparator+client.Name) |
Asim Shankar | 7a72175 | 2014-08-02 14:27:23 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 116 | func (b *googleOAuth) bless(ctx ipc.ServerContext, extension string) (security.WireBlessings, string, error) { |
| 117 | var noblessings security.WireBlessings |
| 118 | self := ctx.LocalPrincipal() |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 119 | if self == nil { |
| 120 | return noblessings, "", fmt.Errorf("server error: no authentication happened") |
| 121 | } |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 122 | var caveat security.Caveat |
| 123 | var err error |
| 124 | if b.revocationManager != nil { |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 125 | caveat, err = b.revocationManager.NewCaveat(self.PublicKey(), b.dischargerLocation) |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 126 | } else { |
| 127 | caveat, err = security.ExpiryCaveat(time.Now().Add(b.duration)) |
| 128 | } |
| 129 | if err != nil { |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 130 | return noblessings, "", err |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 131 | } |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 132 | blessing, err := self.Bless(ctx.RemoteBlessings().PublicKey(), ctx.LocalBlessings(), extension, caveat) |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 133 | if err != nil { |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 134 | return noblessings, "", err |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 135 | } |
Asim Shankar | b18a44f | 2014-10-21 20:25:07 -0700 | [diff] [blame] | 136 | return security.MarshalBlessings(blessing), extension, nil |
Ankur | 3c33d42 | 2014-10-09 11:53:25 -0700 | [diff] [blame] | 137 | } |