accountService.go 4.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
package service

import (
	"context"
	"net/http"
	"os"
	"fmt"
	"encoding/json"
	"github.com/okta/okta-sdk-golang/v2/okta"
	"github.com/okta/okta-sdk-golang/v2/okta/query"
)

type User struct {
14
	Id string `json:"id"`
15
16
17
18
	FirstName string `json:"firstName"`
	LastName string `json:"lastName"`
	Email string `json:"email"`
	Login string `json:"login"`
19
	Password string `json:"password"`
20
21
22
23
24
25
26
}

type Users struct {
	UserList []User `json:"users"`
}

func printUser(user User) {
27
	fmt.Print("\n Firstname:", user.FirstName, "\n Lastname:", user.LastName, "\n Email:", user.Email)
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
}

func printAllUsers(users []User) {
	for index, u := range users {
        fmt.Printf("user at user[%d] is : %v \n", index, u)
	}
}

func GetUsers() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Fetching users...")

		_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		users, _, err := client.User.ListUsers(context.Background(), &query.Params{})
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		allUsers := []User{}

		for _, user := range users {

			User := User{
57
				Id:					fmt.Sprintf("%v", (*user.Profile)["Id"]),
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
				FirstName: 	fmt.Sprintf("%v", (*user.Profile)["firstName"]),
				LastName: 	fmt.Sprintf("%v", (*user.Profile)["lastName"]),
				Email: 			fmt.Sprintf("%v", (*user.Profile)["email"]),
				Login:			fmt.Sprintf("%v", (*user.Profile)["login"]),
			}

			allUsers = append(allUsers, User)
		}

		usr := Users {
			allUsers,
		}

		printAllUsers(allUsers)
		if err := json.NewEncoder(w).Encode(usr); 
		err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}
	}
}

func CreateUser() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Creating new user...")

		_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

90
		tempUserData := User{}
91

92
		if err := json.NewDecoder(r.Body).Decode(&tempUserData); 
93
94
95
96
97
		err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

98
		tempUserData.Login = tempUserData.Email
99
100


101
		p := &okta.PasswordCredential{
102
			Value: tempUserData.Password,
103
104
105
106
107
108
109
		}

		uc := &okta.UserCredentials{
			Password: p,
		}

		profile := okta.UserProfile{}
110
111
112
113
		profile["firstName"] = tempUserData.FirstName
		profile["lastName"] = tempUserData.LastName
		profile["email"] = tempUserData.Email
		profile["login"] = tempUserData.Login
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

		u := &okta.CreateUserRequest{
			Credentials: uc,
			Profile:     &profile,
		}

		qp := query.NewQueryParams(query.WithActivate(true))

		user, _, err := client.User.CreateUser(context.Background(), *u, qp)
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		newUser := User{
				FirstName: fmt.Sprintf("%v", (*user.Profile)["firstName"]),
				LastName: fmt.Sprintf("%v", (*user.Profile)["lastName"]),
				Email: fmt.Sprintf("%v", (*user.Profile)["email"]),
				Login:fmt.Sprintf("%v", (*user.Profile)["login"]),
			}
		
		fmt.Println("User activation success. New user created:")
		printUser(newUser)
137
138

		w.WriteHeader(http.StatusOK)
139
	}
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
}

func RemoveUser() http.HandlerFunc {
	return func(w http.ResponseWriter, r*http.Request) {

		_, client, err := okta.NewClient(context.Background(), okta.WithOrgUrl("https://" + os.Getenv("CLIENT_ID")), okta.WithToken("007XkBhvAfeVf1jUB0xhdwQJJEvWNSBQ6bbO4MoEaV"))
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		tempUserData := User{}

		if err := json.NewDecoder(r.Body).Decode(&tempUserData); 
		err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		fmt.Println("Removing User:", tempUserData.Email)

		// User.Id == email
		// Deactivate user first
		resp, err := client.User.DeactivateUser(context.Background(), tempUserData.Email, &query.Params{})
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}


		// Response.Message?
		if _, err := w.Write([]byte(resp.Self)); err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		// Now Remove user
		response, err := client.User.DeactivateOrDeleteUser(context.Background(), "john-activate@example.com", &query.Params{})
		if err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		if _, err := w.Write([]byte(response.Self)); err != nil {
			ReturnError(w, http.StatusInternalServerError, err.Error())
			return
		}

		fmt.Println("User removal success")
	}
190
}