Skip to content

Commit 183e8ea

Browse files
committed
Git init
0 parents  commit 183e8ea

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

.traefik.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
displayName: NetsocsPlugin
2+
type: middleware
3+
import: github.com/Netsocs-Team/netsocs-traefik-plugin
4+
summary: Netsocs feats plugins
5+
6+
iconPath: foo/icon.png
7+
bannerPath: foo/banner.png
8+
9+
testData:
10+
CookieName: COOKIE_NAME
11+
LicenseApi: http://localhost:8080
12+
AccessControlApi: http://localhost:8080

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Traefik Plugin
2+
This is the Netsocs traefik plugin.

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/Netsocs-Team/netsocs-traefik-plugin
2+
3+
go 1.23.0
4+
5+
toolchain go1.23.7
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
9+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
10+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
11+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
6+
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
7+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
8+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=

main.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)