forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
196 lines (166 loc) · 5.84 KB
/
memory.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2018-2020 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package memory
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/user"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
"github.com/cs3org/reva/pkg/ocm/invite"
"github.com/cs3org/reva/pkg/ocm/invite/manager/registry"
"github.com/cs3org/reva/pkg/ocm/invite/token"
"github.com/cs3org/reva/pkg/rhttp"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
const acceptInviteEndpoint = "invites/accept"
func init() {
registry.Register("memory", New)
}
func (c *config) init() {
if c.Expiration == "" {
c.Expiration = token.DefaultExpirationTime
}
}
// New returns a new invite manager.
func New(m map[string]interface{}) (invite.Manager, error) {
c := &config{}
if err := mapstructure.Decode(m, c); err != nil {
err = errors.Wrap(err, "error creating a new manager")
return nil, err
}
c.init()
return &manager{
Invites: sync.Map{},
AcceptedUsers: sync.Map{},
Config: c,
}, nil
}
type manager struct {
Invites sync.Map
AcceptedUsers sync.Map
Config *config
}
type config struct {
Expiration string `mapstructure:"expiration"`
InsecureConnections bool `mapstructure:"insecure_connections"`
}
func (m *manager) GenerateToken(ctx context.Context) (*invitepb.InviteToken, error) {
ctxUser := user.ContextMustGetUser(ctx)
inviteToken, err := token.CreateToken(m.Config.Expiration, ctxUser.GetId())
if err != nil {
return nil, errors.Wrap(err, "memory: error creating token")
}
m.Invites.Store(inviteToken.GetToken(), inviteToken)
return inviteToken, nil
}
func (m *manager) ForwardInvite(ctx context.Context, invite *invitepb.InviteToken, originProvider *ocmprovider.ProviderInfo) error {
contextUser := user.ContextMustGetUser(ctx)
requestBody := url.Values{
"token": {invite.GetToken()},
"userID": {contextUser.GetId().GetOpaqueId()},
"recipientProvider": {contextUser.GetId().GetIdp()},
"email": {contextUser.GetMail()},
"name": {contextUser.GetDisplayName()},
}
ocmEndpoint, err := getOCMEndpoint(originProvider)
if err != nil {
return err
}
client := rhttp.GetHTTPClient(rhttp.Insecure(m.Config.InsecureConnections))
recipientURL := fmt.Sprintf("%s%s", ocmEndpoint, acceptInviteEndpoint)
req, err := http.NewRequest("POST", recipientURL, strings.NewReader(requestBody.Encode()))
if err != nil {
return errors.Wrap(err, "json: error framing post request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
resp, err := client.Do(req)
if err != nil {
err = errors.Wrap(err, "memory: error sending post request")
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = errors.Wrap(errors.New(resp.Status), "memory: error sending accept post request")
return err
}
return nil
}
func (m *manager) AcceptInvite(ctx context.Context, invite *invitepb.InviteToken, remoteUser *userpb.User) error {
inviteToken, err := m.getTokenIfValid(invite)
if err != nil {
return err
}
currUser := inviteToken.GetUserId().GetOpaqueId()
usersList, ok := m.AcceptedUsers.Load(currUser)
if ok {
acceptedUsers := usersList.([]*userpb.User)
for _, acceptedUser := range acceptedUsers {
if acceptedUser.Id.GetOpaqueId() == remoteUser.Id.OpaqueId && acceptedUser.Id.GetIdp() == remoteUser.Id.Idp {
return errors.New("memory: user already added to accepted users")
}
}
acceptedUsers = append(acceptedUsers, remoteUser)
m.AcceptedUsers.Store(currUser, acceptedUsers)
} else {
acceptedUsers := []*userpb.User{remoteUser}
m.AcceptedUsers.Store(currUser, acceptedUsers)
}
return nil
}
func (m *manager) GetRemoteUser(ctx context.Context, remoteUserID *userpb.UserId) (*userpb.User, error) {
currUser := user.ContextMustGetUser(ctx).GetId().GetOpaqueId()
usersList, ok := m.AcceptedUsers.Load(currUser)
if !ok {
return nil, errtypes.NotFound(remoteUserID.OpaqueId)
}
acceptedUsers := usersList.([]*userpb.User)
for _, acceptedUser := range acceptedUsers {
if (acceptedUser.Id.GetOpaqueId() == remoteUserID.OpaqueId) && (remoteUserID.Idp == "" || acceptedUser.Id.GetIdp() == remoteUserID.Idp) {
return acceptedUser, nil
}
}
return nil, errtypes.NotFound(remoteUserID.OpaqueId)
}
func (m *manager) getTokenIfValid(token *invitepb.InviteToken) (*invitepb.InviteToken, error) {
tokenInterface, ok := m.Invites.Load(token.GetToken())
if !ok {
return nil, errors.New("memory: invalid token")
}
inviteToken := tokenInterface.(*invitepb.InviteToken)
if uint64(time.Now().Unix()) > inviteToken.Expiration.Seconds {
return nil, errors.New("memory: token expired")
}
return inviteToken, nil
}
func getOCMEndpoint(originProvider *ocmprovider.ProviderInfo) (string, error) {
for _, s := range originProvider.Services {
if s.Endpoint.Type.Name == "OCM" {
return s.Endpoint.Path, nil
}
}
return "", errors.New("json: ocm endpoint not specified for mesh provider")
}