Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: go/src/infra/libs/auth/internal/user.go

Issue 1153883002: go: infra/libs/* now live in luci-go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: move the rest too Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/libs/auth/internal/service.go ('k') | go/src/infra/libs/auth/service.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package internal
6
7 import (
8 "fmt"
9 "time"
10
11 "golang.org/x/net/context"
12 "golang.org/x/oauth2"
13 )
14
15 type userAuthTokenProvider struct {
16 oauthTokenProvider
17
18 ctx context.Context
19 config *oauth2.Config
20 }
21
22 // NewUserAuthTokenProvider returns TokenProvider that can perform 3-legged
23 // OAuth flow involving interaction with a user.
24 func NewUserAuthTokenProvider(ctx context.Context, clientID, clientSecret string , scopes []string) (TokenProvider, error) {
25 return &userAuthTokenProvider{
26 oauthTokenProvider: oauthTokenProvider{
27 interactive: true,
28 tokenFlavor: "user",
29 },
30 ctx: ctx,
31 config: &oauth2.Config{
32 ClientID: clientID,
33 ClientSecret: clientSecret,
34 Endpoint: oauth2.Endpoint{
35 AuthURL: "https://accounts.google.com/o/oauth2/ auth",
36 TokenURL: "https://accounts.google.com/o/oauth2/ token",
37 },
38 RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
39 Scopes: scopes,
40 },
41 }, nil
42 }
43
44 func (p *userAuthTokenProvider) MintToken() (Token, error) {
45 // Grab the authorization code by redirecting a user to a consent screen .
46 url := p.config.AuthCodeURL("", oauth2.AccessTypeOffline, oauth2.Approva lForce)
47 fmt.Printf("Visit the URL to get authorization code.\n\n%s\n\n", url)
48 fmt.Printf("Authorization code: ")
49 var code string
50 if _, err := fmt.Scan(&code); err != nil {
51 return nil, err
52 }
53 // Exchange it for a token.
54 tok, err := p.config.Exchange(p.ctx, code)
55 if err != nil {
56 return nil, err
57 }
58 return makeToken(tok), nil
59 }
60
61 func (p *userAuthTokenProvider) RefreshToken(tok Token) (Token, error) {
62 // Clear expiration time to force token refresh. Do not use 0 since it m eans
63 // that token never expires.
64 t := extractOAuthToken(tok)
65 t.Expiry = time.Unix(1, 0)
66 src := p.config.TokenSource(p.ctx, &t)
67 newTok, err := src.Token()
68 if err != nil {
69 return nil, err
70 }
71 return makeToken(newTok), nil
72 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/auth/internal/service.go ('k') | go/src/infra/libs/auth/service.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698