|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// Config the plugin configuration. |
| 12 | +type Config struct { |
| 13 | + AccessControlApi string |
| 14 | + CookieName string |
| 15 | + LicenseApi string |
| 16 | +} |
| 17 | + |
| 18 | +// CreateConfig creates the default plugin configuration. |
| 19 | +func CreateConfig() *Config { |
| 20 | + return &Config{} |
| 21 | +} |
| 22 | + |
| 23 | +type Person struct { |
| 24 | + ID string `json:"id"` |
| 25 | + Name string `json:"name"` |
| 26 | + Photo string `json:"photo"` |
| 27 | + Type string `json:"type"` |
| 28 | + IdentificationDocument string `json:"identification_document"` |
| 29 | + IdentificationDocumentType string `json:"identification_document_type"` |
| 30 | + Email string `json:"email"` |
| 31 | + AccessLevels []interface{} `json:"access_levels"` |
| 32 | + Phone string `json:"phone"` |
| 33 | + Vehicles []interface{} `json:"vehicles"` |
| 34 | + ActivationDate string `json:"activation_date"` |
| 35 | + ExpirationDate string `json:"expiration_date"` |
| 36 | + Lists []interface{} `json:"lists"` |
| 37 | + Signature string `json:"signature"` |
| 38 | + Attachments []interface{} `json:"attachments"` |
| 39 | + Departments []interface{} `json:"departments"` |
| 40 | + Disabled bool `json:"disabled"` |
| 41 | + CreatedAt time.Time `json:"created_at"` |
| 42 | + UpdatedAt time.Time `json:"updated_at"` |
| 43 | +} |
| 44 | + |
| 45 | +type NetsocsUserSession struct { |
| 46 | + next http.Handler |
| 47 | + AccessControlApi string |
| 48 | + CookieName string |
| 49 | + LicenseApi string |
| 50 | +} |
| 51 | + |
| 52 | +// New created a new Demo plugin. |
| 53 | +func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { |
| 54 | + return &NetsocsUserSession{ |
| 55 | + next: next, |
| 56 | + AccessControlApi: config.AccessControlApi, |
| 57 | + CookieName: config.CookieName, |
| 58 | + LicenseApi: config.LicenseApi, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (a *NetsocsUserSession) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
| 63 | + license := CheckLicense(a.LicenseApi) |
| 64 | + if !license { |
| 65 | + http.Redirect(rw, req, "/n/config", http.StatusFound) |
| 66 | + return |
| 67 | + } |
| 68 | + err := SyncUserSession(req.Context(), req, rw, a.CookieName, a.AccessControlApi) |
| 69 | + if err != nil { |
| 70 | + http.Redirect(rw, req, "/", http.StatusFound) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + a.next.ServeHTTP(rw, req) |
| 75 | +} |
| 76 | + |
| 77 | +func SyncUserSession(ctx context.Context, req *http.Request, rw http.ResponseWriter, |
| 78 | + cookieName string, accessControlApi string) error { |
| 79 | + cookie, err := req.Cookie(cookieName) |
| 80 | + if err == nil { |
| 81 | + user, err := CheckUser(cookie.Value, accessControlApi, cookieName) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("error checking user: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + if len(user.Departments) == 0 { |
| 87 | + http.Redirect(rw, req, "/n/access_control/host", http.StatusFound) |
| 88 | + } |
| 89 | + |
| 90 | + } else if err == http.ErrNoCookie { |
| 91 | + return fmt.Errorf("cookie not found") |
| 92 | + } else { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func CheckLicense(licenseApi string) bool { |
| 100 | + url := fmt.Sprintf("%s/license", licenseApi) |
| 101 | + req, err := http.NewRequest("GET", url, nil) |
| 102 | + if err != nil { |
| 103 | + return false |
| 104 | + } |
| 105 | + client := &http.Client{} |
| 106 | + resp, err := client.Do(req) |
| 107 | + if err != nil { |
| 108 | + return false |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + return resp.StatusCode == http.StatusOK |
| 112 | +} |
| 113 | + |
| 114 | +func CheckUser(netsocstoken string, accesControlApi string, cookieName string) (Person, error) { |
| 115 | + url := fmt.Sprintf("%s/check_user", accesControlApi) |
| 116 | + req, err := http.NewRequest("GET", url, nil) |
| 117 | + if err != nil { |
| 118 | + return Person{}, err |
| 119 | + } |
| 120 | + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", cookieName, netsocstoken)) |
| 121 | + client := &http.Client{} |
| 122 | + resp, err := client.Do(req) |
| 123 | + if err != nil { |
| 124 | + return Person{}, err |
| 125 | + } |
| 126 | + defer resp.Body.Close() |
| 127 | + if resp.StatusCode != http.StatusOK { |
| 128 | + return Person{}, fmt.Errorf("failed to check user: %s", resp.Status) |
| 129 | + } |
| 130 | + var user Person |
| 131 | + err = json.NewDecoder(resp.Body).Decode(&user) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + return Person{}, err |
| 135 | + } |
| 136 | + if user.ID == "" { |
| 137 | + return Person{}, fmt.Errorf("user not found") |
| 138 | + } |
| 139 | + return user, nil |
| 140 | + |
| 141 | +} |
0 commit comments