/
sessions.go
148 lines (124 loc) · 4.22 KB
/
sessions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"github.com/gravitational/trace"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/gravitational/teleport/api/types"
)
// GetWebSession returns the web session for the specified request.
// Implements ReadAccessPoint
func (c *Client) GetWebSession(ctx context.Context, req types.GetWebSessionRequest) (types.WebSession, error) {
return c.WebSessions().Get(ctx, req)
}
// WebSessions returns the web sessions controller
func (c *Client) WebSessions() types.WebSessionInterface {
return &webSessions{c: c}
}
// Get returns the web session for the specified request
func (r *webSessions) Get(ctx context.Context, req types.GetWebSessionRequest) (types.WebSession, error) {
resp, err := r.c.grpc.GetWebSession(ctx, &req)
if err != nil {
return nil, trace.Wrap(err)
}
return resp.Session, nil
}
// List returns the list of all web sessions
func (r *webSessions) List(ctx context.Context) ([]types.WebSession, error) {
resp, err := r.c.grpc.GetWebSessions(ctx, &emptypb.Empty{})
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]types.WebSession, 0, len(resp.Sessions))
for _, session := range resp.Sessions {
out = append(out, session)
}
return out, nil
}
// Upsert not implemented: can only be called locally.
func (r *webSessions) Upsert(ctx context.Context, session types.WebSession) error {
return trace.NotImplemented(notImplementedMessage)
}
// Delete deletes the web session specified with the request
func (r *webSessions) Delete(ctx context.Context, req types.DeleteWebSessionRequest) error {
_, err := r.c.grpc.DeleteWebSession(ctx, &req)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// DeleteAll deletes all web sessions
func (r *webSessions) DeleteAll(ctx context.Context) error {
_, err := r.c.grpc.DeleteAllWebSessions(ctx, &emptypb.Empty{})
if err != nil {
return trace.Wrap(err)
}
return nil
}
type webSessions struct {
c *Client
}
// GetWebToken returns the web token for the specified request.
// Implements ReadAccessPoint
func (c *Client) GetWebToken(ctx context.Context, req types.GetWebTokenRequest) (types.WebToken, error) {
return c.WebTokens().Get(ctx, req)
}
// WebTokens returns the web tokens controller
func (c *Client) WebTokens() types.WebTokenInterface {
return &webTokens{c: c}
}
// Get returns the web token for the specified request
func (r *webTokens) Get(ctx context.Context, req types.GetWebTokenRequest) (types.WebToken, error) {
resp, err := r.c.grpc.GetWebToken(ctx, &req)
if err != nil {
return nil, trace.Wrap(err)
}
return resp.Token, nil
}
// List returns the list of all web tokens
func (r *webTokens) List(ctx context.Context) ([]types.WebToken, error) {
resp, err := r.c.grpc.GetWebTokens(ctx, &emptypb.Empty{})
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]types.WebToken, 0, len(resp.Tokens))
for _, token := range resp.Tokens {
out = append(out, token)
}
return out, nil
}
// Upsert not implemented: can only be called locally.
func (r *webTokens) Upsert(ctx context.Context, token types.WebToken) error {
return trace.NotImplemented(notImplementedMessage)
}
// Delete deletes the web token specified with the request
func (r *webTokens) Delete(ctx context.Context, req types.DeleteWebTokenRequest) error {
_, err := r.c.grpc.DeleteWebToken(ctx, &req)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// DeleteAll deletes all web tokens
func (r *webTokens) DeleteAll(ctx context.Context) error {
_, err := r.c.grpc.DeleteAllWebTokens(ctx, &emptypb.Empty{})
if err != nil {
return trace.Wrap(err)
}
return nil
}
type webTokens struct {
c *Client
}
const notImplementedMessage = "not implemented: can only be called by auth locally"