-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (93 loc) · 2.91 KB
/
main.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
package main
import (
"context"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/errors"
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/go-oauth2/oauth2/v4/server"
"github.com/go-resty/resty/v2"
"github.com/jackc/pgx/v4"
"github.com/tidwall/gjson"
pg "github.com/vgarvardt/go-oauth2-pg/v4"
"github.com/vgarvardt/go-pg-adapter/pgx4adapter"
"log"
"net/http"
"strings"
"time"
)
var client = resty.New()
func main() {
pgxConn, _ := pgx.Connect(context.TODO(), "postgres://postgres:postgres@postgres:5432/postgres")
manager := manage.NewDefaultManager()
adapter := pgx4adapter.NewConn(pgxConn)
tokenStore, _ := pg.NewTokenStore(adapter, pg.WithTokenStoreGCInterval(time.Minute))
defer tokenStore.Close()
// client pg store
clientStore, _ := pg.NewClientStore(adapter)
clientStore.Create(&models.Client{
ID: "222222",
Secret: "22222222",
Domain: "http://localhost:9096",
})
manager.MapTokenStorage(tokenStore)
manager.MapClientStorage(clientStore)
srv := server.NewServer(server.NewConfig(), manager)
srv.SetAllowedGrantType(oauth2.PasswordCredentials)
srv.SetAllowGetAccessRequest(true)
srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {
log.Println("Internal Error:", err.Error())
return
})
srv.SetResponseErrorHandler(func(re *errors.Response) {
log.Println("Response Error:", re.Error.Error())
})
srv.SetPasswordAuthorizationHandler(func(username, password string) (userID string, err error) {
resp, err := client.R().
SetBody(map[string]interface{}{
"username": username,
"password": password,
}).Post("http://user:8080/users/oauth")
if err != nil {
log.Println("ERROR sending the request")
return
}
if resp.StatusCode() == 200 {
userID = gjson.Get(resp.String(), "username").String()
}
return
})
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
srv.HandleTokenRequest(w, r)
})
http.HandleFunc("/check/", func(w http.ResponseWriter, r *http.Request) {
token, err := srv.ValidationBearerToken(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
originalPath := r.Header.Get("x-envoy-original-path")
log.Println("originalPath: " + originalPath)
//if the request is for the cloud agent
if strings.HasPrefix(originalPath, "/agent/api") {
resp, err := client.R().
SetQueryParams(map[string]string{
"username": token.GetUserID(),
}).Get("http://user:8080/users")
if err != nil {
log.Println("ERROR sending the request")
return
}
var acapyToken string
if resp.StatusCode() == 200 {
acapyToken = gjson.Get(resp.String(), "acapyToken").String()
log.Println("acapyToken: " + acapyToken)
} else {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
w.Header().Set("Authorization", "Bearer "+acapyToken)
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}