-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (105 loc) · 3.23 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/Nerzal/gocloak/v13"
"github.com/gorilla/mux"
)
type Response struct {
Message string `json:"message"`
Response interface{} `json:"response"`
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", good).Methods("GET")
r.HandleFunc("/create", CreateKeycloakUser).Methods("GET")
r.HandleFunc("/login", LoginKeycloakUser).Methods("POST")
fmt.Println("Server is starting on port 3000...")
if err := http.ListenAndServe(":3000", r); err != nil {
fmt.Println("Error starting server:", err)
}
}
type SignInRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
func good(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(Response{
Message: "logado com sucesso",
})
}
func LoginKeycloakUser(w http.ResponseWriter, r *http.Request) {
var data SignInRequest
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
fmt.Println("Error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
client := gocloak.NewClient("http://localhost:8080")
ctx := context.Background()
token, err := client.Login(ctx, "rest-golang", "gUGw81NmB4n8X5k5wiy9XHvTlEdwrGir", "master", data.Username, data.Password)
if err != nil {
fmt.Println("Error logging in admin:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response := Response{
Message: "Server is up and running",
Response: token,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
fmt.Println("Error encoding response:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func CreateKeycloakUser(w http.ResponseWriter, r *http.Request) {
client := gocloak.NewClient("http://localhost:8080")
ctx := context.Background()
token, err := client.LoginAdmin(ctx, "admin", "admin", "master")
if err != nil {
fmt.Println("Error logging in admin:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if token == nil {
fmt.Println("Received nil token")
http.Error(w, "Received nil token", http.StatusInternalServerError)
return
}
user := gocloak.User{
FirstName: gocloak.StringP("Elisio"),
LastName: gocloak.StringP("Mualumene"),
Email: gocloak.StringP("[email protected]"),
Enabled: gocloak.BoolP(true),
Username: gocloak.StringP("elisiomualumene"),
}
userID, err := client.CreateUser(ctx, token.AccessToken, "master", user)
if err != nil {
fmt.Println("Error creating user:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("Created user ID:", userID)
password := "admin"
err = client.SetPassword(ctx, token.AccessToken, userID, "master", password, false)
if err != nil {
fmt.Println("Error setting password:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response := Response{
Message: "Server is up and running",
Response: token,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
fmt.Println("Error encoding response:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}