forked from alekcz/charmander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.clj
174 lines (152 loc) · 7.42 KB
/
admin.clj
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
132
133
134
135
136
137
138
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
(ns charmander.admin
(:require [clojure.java.io :as io]
[clojure.walk :as w]
[environ.core :refer [env]]
[googlecredentials.core :as gcred])
(:import com.google.auth.oauth2.GoogleCredentials
com.google.auth.oauth2.ServiceAccountCredentials
com.google.firebase.FirebaseApp
com.google.firebase.FirebaseOptions
com.google.firebase.FirebaseOptions$Builder
com.google.firebase.auth.FirebaseAuth
com.google.firebase.auth.FirebaseAuthException
com.google.firebase.auth.UserRecord
com.google.firebase.auth.UserRecord$CreateRequest
com.google.firebase.auth.UserRecord$UpdateRequest
;firestore
com.google.firebase.cloud.FirestoreClient
com.google.cloud.firestore.Firestore
com.google.cloud.firestore.QueryDocumentSnapshot
com.google.cloud.firestore.QuerySnapshot
com.google.cloud.firestore.DocumentReference
com.google.cloud.firestore.DocumentSnapshot
com.google.cloud.firestore.WriteResult
com.google.api.core.ApiFuture
com.google.cloud.firestore.CollectionReference)
(:gen-class))
; private methods
(defn- build-firebase-options
([]
(try
(-> (new FirebaseOptions$Builder) ;use thread-first when the final part of the function will return value to be used
(.setCredentials (gcred/load-service-credentials))
(.build))
(catch Exception e (println "\nError: FIREBASE_CONFIG/GOOGLE_APPLICATION_CREDENTIALS AND GOOGLE_CLOUD_PROJECT environment variables must both be set"))))
([database-name]
(try
(-> (new FirebaseOptions$Builder) ;use thread-first when the final part of the function will return value to be used
(.setCredentials ^ServiceAccountCredentials (gcred/load-service-credentials))
(.setDatabaseUrl (str "https://" database-name ".firebaseio.com"))
(.build))
(catch Exception e (println "\nError: FIREBASE_CONFIG/GOOGLE_APPLICATION_CREDENTIALS AND GOOGLE_CLOUD_PROJECT environment variables must both be set")))))
(defn- build-create-user-request [email password]
(let [create-request (new UserRecord$CreateRequest)]
(doto create-request ;doto mutates the object. Use it when you're going to return the object
(.setEmail email)
(.setPassword password)
(.setEmailVerified false)
(.setDisabled false))))
(defn- convert-user-record-to-map [^UserRecord user-record]
{ :email (. user-record getEmail)
:email-verified (. user-record isEmailVerified)
:uid (. user-record getUid)
:provider-id (. user-record getProviderId)
:photo-url (. user-record getPhotoUrl)
:phone-number (. user-record getPhoneNumber)
:display-name (. user-record getDisplayName)
:disabled (. user-record isDisabled)
:custom-claims (. user-record getCustomClaims)})
(defn- format-error [error]
(. error getMessage))
; public methods
; init admin api
(defn init []
(let [database-name (or (env :firebase-database) (env :google-cloud-project))]
(if (nil? database-name)
(try
(. FirebaseAuth getInstance)
(catch IllegalStateException ise
(let [options (build-firebase-options)]
(. FirebaseApp initializeApp options)))))
(try
(. FirebaseAuth getInstance)
(catch IllegalStateException ise
(let [options (build-firebase-options database-name)]
(. FirebaseApp initializeApp options))))))
; user management api
(defn create-user [email password]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
create-request (build-create-user-request email password)]
(convert-user-record-to-map (. firebase-auth createUser create-request)))
(catch Exception e {:error true :error-data (format-error e)})))
(defn delete-user [uuid]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(. firebase-auth deleteUser uuid))
(catch Exception e {:error true :error-data (format-error e)})))
(defn get-user [uuid]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(convert-user-record-to-map (. firebase-auth getUser uuid)))
(catch Exception e {:error true :error-data (format-error e)})))
(defn get-user-by-email [email]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(convert-user-record-to-map (. firebase-auth getUserByEmail email)))
(catch Exception e {:error true :error-data (format-error e)})))
(defn get-user-by-phone-number [phone-number]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(convert-user-record-to-map (. firebase-auth getUserByPhoneNumber phone-number)))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-user-email [uuid email]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setEmail email) (.setEmailVerified false)))))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-user-password [uuid password]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPassword password)))))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-user-phone-number [uuid phone-number]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPhoneNumber phone-number)))))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-user-display-name [uuid display-name]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setDisplayName display-name)))))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-user-photo-url [uuid photo-url]
(try
(let [firebase-auth (. FirebaseAuth getInstance)
update-request (new UserRecord$UpdateRequest uuid)]
(convert-user-record-to-map (. firebase-auth updateUser (doto update-request (.setPhotoUrl photo-url)))))
(catch Exception e {:error true :error-data (format-error e)})))
(defn generate-password-reset-link [email]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(. firebase-auth generatePasswordResetLink email))
(catch Exception e {:error true :error-data (format-error e)})))
(defn generate-email-verification-link [email]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(. firebase-auth generateEmailVerificationLink email))
(catch Exception e {:error true :error-data (format-error e)})))
(defn generate-custom-token [uuid]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(. firebase-auth createCustomToken uuid))
(catch Exception e {:error true :error-data (format-error e)})))
(defn set-custom-user-claims [uuid ^clojure.lang.PersistentHashMap claims]
(try
(let [firebase-auth (. FirebaseAuth getInstance)]
(. firebase-auth setCustomUserClaims uuid (java.util.HashMap. claims)))
(catch Exception e {:error true :error-data (format-error e)})))