github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/sessions.go (about)

     1  /*
     2  Copyright 2021 Gravitational, 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  package client
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/gravitational/trace"
    23  	"google.golang.org/protobuf/types/known/emptypb"
    24  
    25  	"github.com/gravitational/teleport/api/types"
    26  )
    27  
    28  // GetWebSession returns the web session for the specified request.
    29  // Implements ReadAccessPoint
    30  func (c *Client) GetWebSession(ctx context.Context, req types.GetWebSessionRequest) (types.WebSession, error) {
    31  	return c.WebSessions().Get(ctx, req)
    32  }
    33  
    34  // WebSessions returns the web sessions controller
    35  func (c *Client) WebSessions() types.WebSessionInterface {
    36  	return &webSessions{c: c}
    37  }
    38  
    39  // Get returns the web session for the specified request
    40  func (r *webSessions) Get(ctx context.Context, req types.GetWebSessionRequest) (types.WebSession, error) {
    41  	resp, err := r.c.grpc.GetWebSession(ctx, &req)
    42  	if err != nil {
    43  		return nil, trace.Wrap(err)
    44  	}
    45  	return resp.Session, nil
    46  }
    47  
    48  // List returns the list of all web sessions
    49  func (r *webSessions) List(ctx context.Context) ([]types.WebSession, error) {
    50  	resp, err := r.c.grpc.GetWebSessions(ctx, &emptypb.Empty{})
    51  	if err != nil {
    52  		return nil, trace.Wrap(err)
    53  	}
    54  	out := make([]types.WebSession, 0, len(resp.Sessions))
    55  	for _, session := range resp.Sessions {
    56  		out = append(out, session)
    57  	}
    58  	return out, nil
    59  }
    60  
    61  // Upsert not implemented: can only be called locally.
    62  func (r *webSessions) Upsert(ctx context.Context, session types.WebSession) error {
    63  	return trace.NotImplemented(notImplementedMessage)
    64  }
    65  
    66  // Delete deletes the web session specified with the request
    67  func (r *webSessions) Delete(ctx context.Context, req types.DeleteWebSessionRequest) error {
    68  	_, err := r.c.grpc.DeleteWebSession(ctx, &req)
    69  	if err != nil {
    70  		return trace.Wrap(err)
    71  	}
    72  	return nil
    73  }
    74  
    75  // DeleteAll deletes all web sessions
    76  func (r *webSessions) DeleteAll(ctx context.Context) error {
    77  	_, err := r.c.grpc.DeleteAllWebSessions(ctx, &emptypb.Empty{})
    78  	if err != nil {
    79  		return trace.Wrap(err)
    80  	}
    81  	return nil
    82  }
    83  
    84  type webSessions struct {
    85  	c *Client
    86  }
    87  
    88  // GetWebToken returns the web token for the specified request.
    89  // Implements ReadAccessPoint
    90  func (c *Client) GetWebToken(ctx context.Context, req types.GetWebTokenRequest) (types.WebToken, error) {
    91  	return c.WebTokens().Get(ctx, req)
    92  }
    93  
    94  // WebTokens returns the web tokens controller
    95  func (c *Client) WebTokens() types.WebTokenInterface {
    96  	return &webTokens{c: c}
    97  }
    98  
    99  // Get returns the web token for the specified request
   100  func (r *webTokens) Get(ctx context.Context, req types.GetWebTokenRequest) (types.WebToken, error) {
   101  	resp, err := r.c.grpc.GetWebToken(ctx, &req)
   102  	if err != nil {
   103  		return nil, trace.Wrap(err)
   104  	}
   105  	return resp.Token, nil
   106  }
   107  
   108  // List returns the list of all web tokens
   109  func (r *webTokens) List(ctx context.Context) ([]types.WebToken, error) {
   110  	resp, err := r.c.grpc.GetWebTokens(ctx, &emptypb.Empty{})
   111  	if err != nil {
   112  		return nil, trace.Wrap(err)
   113  	}
   114  	out := make([]types.WebToken, 0, len(resp.Tokens))
   115  	for _, token := range resp.Tokens {
   116  		out = append(out, token)
   117  	}
   118  	return out, nil
   119  }
   120  
   121  // Upsert not implemented: can only be called locally.
   122  func (r *webTokens) Upsert(ctx context.Context, token types.WebToken) error {
   123  	return trace.NotImplemented(notImplementedMessage)
   124  }
   125  
   126  // Delete deletes the web token specified with the request
   127  func (r *webTokens) Delete(ctx context.Context, req types.DeleteWebTokenRequest) error {
   128  	_, err := r.c.grpc.DeleteWebToken(ctx, &req)
   129  	if err != nil {
   130  		return trace.Wrap(err)
   131  	}
   132  	return nil
   133  }
   134  
   135  // DeleteAll deletes all web tokens
   136  func (r *webTokens) DeleteAll(ctx context.Context) error {
   137  	_, err := r.c.grpc.DeleteAllWebTokens(ctx, &emptypb.Empty{})
   138  	if err != nil {
   139  		return trace.Wrap(err)
   140  	}
   141  	return nil
   142  }
   143  
   144  type webTokens struct {
   145  	c *Client
   146  }
   147  
   148  const notImplementedMessage = "not implemented: can only be called by auth locally"