// ObtainToken uses an interactive browser session to obtain an oauth.Token
// for the given OAuther.
432081
func (oauther *OAuther) ObtainToken() (err error) {
	oauther.codeChannel = make(chan string)
	oauther.errorChannel = make(chan error)

	oauther.runServerIfNecessary()

	transport := oauther.Transport()

	// Get an authorization code from the data provider.
	// ("Please ask the user if I can access this resource.")
	authCodeUrl, _ := url.QueryUnescape(oauther.config.AuthCodeURL(""))
	webbrowser.Open(authCodeUrl)

	select {
	case authCode := <-oauther.codeChannel:
		// Exchange the authorization code for an access token.
		// ("Here's the code you gave the user, now give me a token!")
		oauther.Token, err = transport.Exchange(authCode)
		return
	case err = <-oauther.errorChannel:
		return
	}
}
507115
func (web *webAPIHandlers) GetAuth(r *http.Request, args *WebGenericArgs, reply *GetAuthReply) error {
	_, owner, authErr := webRequestAuthenticate(r)
	if authErr != nil {
		return toJSONError(authErr)
	}
	if !owner {
		return toJSONError(errAccessDenied)
	}
	creds := globalServerConfig.GetCredential()
	reply.AccessKey = creds.AccessKey
	reply.SecretKey = creds.SecretKey
	reply.UIVersion = browser.UIVersion
	return nil
}
45727
func (user *User) OAuthToken() string {
	// todo: oauthtoken per host?
	/*
		host := user.ctx.ServiceBaseURL.Host

		if host == "" {
			host = user.ctx.Service().DefaultBaseURL.Host
		}
	*/
	ps, _ := user.protectedSettings()

	if ps != nil {
		if ps.OAuthExpireDate != nil && ps.OAuthExpireDate.Before(time.Now().Add(time.Second*5)) {
			token, err := user.ctx.OAuthProvider().OAuth2Client(user.ctx).TokenSource(oauth2.NoContext, &oauth2.Token{AccessToken: ps.OAuthToken, Expiry: *ps.OAuthExpireDate, RefreshToken: ps.OAuthRefreshToken}).Token()

			if err != nil {
				if strings.Contains(err.Error(), "revoked") || strings.Contains(err.Error(), "invalid_grant") {
					//remove stored OAuthToken
					//todo: provide revoked callback
					ps.OAuthToken = ""
					ps.OAuthExpireDate = nil
					user.saveProtectedSettings()
				}
				user.ctx.Log().WithError(err).Error("OAuth token refresh failed")
				return ""
			}

			if token != nil {
				ps.OAuthToken = token.AccessToken
				if token.RefreshToken != "" {
					ps.OAuthRefreshToken = token.RefreshToken
				}
				ps.OAuthExpireDate = &token.Expiry
				user.saveProtectedSettings()

			}

		}
		return ps.OAuthToken
	}

	return ""
}
504491
func GetJWTToken(proj *v1alpha1.AppProject, roleName string, issuedAt int64) (*v1alpha1.JWTToken, int, error) {
	role, _, err := GetRoleByName(proj, roleName)
	if err != nil {
		return nil, -1, err
	}
	for i, token := range role.JWTTokens {
		if issuedAt == token.IssuedAt {
			return &token, i, nil
		}
	}
	return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", role.Name, issuedAt, proj.Name)
}
818486
func (c *Client) GetTokens(opts ...services.MarshalOption) ([]services.ProvisionToken, error) {
	out, err := c.Get(c.Endpoint("tokens"), url.Values{})
	if err != nil {
		return nil, trace.Wrap(err)
	}
	var tokens []services.ProvisionTokenV1
	if err := json.Unmarshal(out.Bytes(), &tokens); err != nil {
		return nil, trace.Wrap(err)
	}
	return services.ProvisionTokensFromV1(tokens), nil
}
534477
func GetTokenFromHOTPMockFile(path string) (token string, e error) {
	otp, err := LoadHOTPMockFromFile(path)
	if err != nil {
		return "", trace.Wrap(err)
	}

	token = otp.OTP()

	err = otp.SaveToFile(path)
	if err != nil {
		return "", trace.Wrap(err)
	}

	return token, nil
}
583768
func (s *APIServer) getTokens(auth ClientI, w http.ResponseWriter, r *http.Request, p httprouter.Params, version string) (interface{}, error) {
	tokens, err := auth.GetTokens()
	if err != nil {
		return nil, trace.Wrap(err)
	}
	return services.ProvisionTokensToV1(tokens), nil
}
1322577
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*oauth2.Token, error) {
	tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.TokenURL, v)
	if err != nil {
		return nil, err
	}
	return tokenFromInternal(tk), nil
}
112560
func (c *Client) GetAuth(accessToken string) (resp GetAuthResponse, err error) {
	options := GetAuthOptions{
		AccountIDs: []string{},
	}
	return c.GetAuthWithOptions(accessToken, options)
}
1359336
func (a *ACL) GetToken(args *structs.ACLTokenSpecificRequest, reply *structs.SingleACLTokenResponse) error {
	if !a.srv.config.ACLEnabled {
		return aclDisabled
	}
	if done, err := a.srv.forward("ACL.GetToken", args, args, reply); done {
		return err
	}
	defer metrics.MeasureSince([]string{"nomad", "acl", "get_token"}, time.Now())

	acl, err := a.srv.ResolveToken(args.AuthToken)
	if err != nil {
		return err
	}

	// Ensure ACLs are enabled and this call is made with one
	if acl == nil {
		return structs.ErrPermissionDenied
	}

	// Setup the blocking query
	opts := blockingOptions{
		queryOpts: &args.QueryOptions,
		queryMeta: &reply.QueryMeta,
		run: func(ws memdb.WatchSet, state *state.StateStore) error {
			// Look for the token
			out, err := state.ACLTokenByAccessorID(ws, args.AccessorID)
			if err != nil {
				return err
			}

			if out == nil {
				// If the token doesn't resolve, only allow management tokens to
				// block.
				if !acl.IsManagement() {
					return structs.ErrPermissionDenied
				}

				// Check management level permissions or that the secret ID matches the
				// accessor ID
			} else if !acl.IsManagement() && out.SecretID != args.AuthToken {
				return structs.ErrPermissionDenied
			}

			// Setup the output
			reply.Token = out
			if out != nil {
				reply.Index = out.ModifyIndex
			} else {
				// Use the last index that affected the token table
				index, err := state.Index("acl_token")
				if err != nil {
					return err
				}
				reply.Index = index
			}
			return nil
		}}
	return a.srv.blockingRPC(&opts)
}
875393
func (t *TokenProvider) GetToken(audience string) (*auth.Token, error) {
	signature, expiry := t.signer.SignWithDuration(audience, 2*time.Hour)
	return auth.NewToken(auth.CBSTokenTypeSAS, signature, expiry), nil
}
904662
func (c *UserConfig) GetToken() (string, string) {
	if c.AccessTokenKey != "" {
		return c.AccessTokenKey, c.AccessTokenSecret
	}
	if c.RequestTokenKey != "" {
		return c.RequestTokenKey, c.RequestTokenSecret
	}
	return "", ""
}
379667
func (self *TokenClient) Retrieve(id string) (*Token, error) {
	token := Token{}
	path := "/v1/tokens/" + url.QueryEscape(id)
	err := query("GET", path, nil, &token)
	return &token, err
}
782827
func (f *tokenFuture) Get() string {
	f.m.Lock()
	defer f.m.Unlock()
	return f.token
}
1371894
func GetToken(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	t, err := Token()
	if err != nil {
		return "", err
	}
	return string(t), err
}
1152194
func GetTokens(username, password string) (string, *http.Response, string, []error) {
	k := kumoru.New()

	token := uuid.New()

	k.Put(fmt.Sprintf("%v/v1/tokens/%v", k.EndPoint.Authorization, token))
	k.SetBasicAuth(username, password)
	resp, body, errs := k.End()

	return token, resp, body, errs
}
704091
func (tp *TokenPool) Get() *Token {
	if len(tp.pool) < 1 {
		return new(Token)
	}

	t := tp.pool[len(tp.pool)-1]
	tp.pool = tp.pool[:len(tp.pool)-1]
	return t
}