-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynapse.go
More file actions
72 lines (59 loc) · 1.61 KB
/
synapse.go
File metadata and controls
72 lines (59 loc) · 1.61 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type SynapseConfig struct {
ApiUrl string
AccessToken string
Domain string
}
type Synapse struct {
client *http.Client
config *SynapseConfig
}
func NewSynapse(cfg *SynapseConfig) *Synapse {
client := &http.Client{}
return &Synapse{
client: client,
config: cfg,
}
}
func (s *Synapse) DeprovisionUser(email string) error {
// Extract the userId from the email
userId := strings.Split(email, "@")[0]
domain := strings.Split(email, "@")[1]
if domain != s.config.Domain {
return fmt.Errorf("domain not allowed: %s", domain)
}
body := map[string]any{
"erase": true,
}
jsonData, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %v", err)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/deactivate/@%s:%s", s.config.ApiUrl, userId, domain), bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}
s.prepareRequest(req)
res, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("failed to deprovision user: %v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNotFound {
return fmt.Errorf("failed to deprovision user, status code: %d", res.StatusCode)
}
return nil
}
func (s *Synapse) prepareRequest(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.config.AccessToken))
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "UserliWebhookListener/1.0")
req.Header.Set("ocs-apirequest", "true")
}