github.com/openshift-online/ocm-sdk-go@v0.1.473/token.go (about)

     1  /*
     2  Copyright (c) 2018 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file contains the implementations of the methods of the connection that handle OpenID
    18  // authentication tokens.
    19  
    20  package sdk
    21  
    22  import (
    23  	"context"
    24  
    25  	"time"
    26  )
    27  
    28  // Tokens returns the access and refresh tokens that are currently in use by the connection. If it
    29  // is necessary to request new tokens because they weren't requested yet, or because they are
    30  // expired, this method will do it and will return an error if it fails.
    31  //
    32  // If new tokens are needed the request will be retried with an exponential backoff.
    33  //
    34  // This operation is potentially lengthy, as it may require network communication. Consider using a
    35  // context and the TokensContext method.
    36  // The returned access and refresh tokens are empty strings if the
    37  // connection does not use authentication. In that case no error is
    38  // returned either
    39  func (c *Connection) Tokens(expiresIn ...time.Duration) (access, refresh string, err error) {
    40  	if len(expiresIn) == 1 {
    41  		access, refresh, err = c.TokensContext(context.Background(), expiresIn[0])
    42  	} else {
    43  		access, refresh, err = c.TokensContext(context.Background())
    44  	}
    45  	return
    46  
    47  }
    48  
    49  // TokensContext returns the access and refresh tokens that are currently in use by the connection.
    50  // If it is necessary to request new tokens because they weren't requested yet, or because they are
    51  // expired, this method will do it and will return an error if it fails.
    52  //
    53  // If new tokens are needed the request will be retried with an exponential backoff.
    54  // The returned access and refresh tokens are empty strings if the
    55  // connection does not use authentication. In that case no error is
    56  // returned either
    57  func (c *Connection) TokensContext(ctx context.Context, expiresIn ...time.Duration) (access,
    58  	refresh string, err error) {
    59  	if c.authnWrapper != nil {
    60  		access, refresh, err = c.authnWrapper.Tokens(ctx, expiresIn...)
    61  	}
    62  	return
    63  }