76 ui_set_group(ui->ctx, APP_STATE_BROWSER_SESSION); |
76 ui_set_group(ui->ctx, APP_STATE_BROWSER_SESSION); |
77 } |
77 } |
78 |
78 |
79 // ------------------------------ davbrowser_connect2repo ------------------------------ |
79 // ------------------------------ davbrowser_connect2repo ------------------------------ |
80 |
80 |
81 void davbrowser_connect2repo(UiObject *ui, DavBrowser *browser, DavCfgRepository *repo, const char *path) { |
81 typedef struct DavConnect2Repo { |
|
82 UiObject *ui; |
|
83 DavBrowser *browser; |
|
84 |
|
85 DavCfgRepository *repo; |
|
86 char *path; |
|
87 |
|
88 UiString *password; |
|
89 } DavConnect2Repo; |
|
90 |
|
91 static void dialog_secretstore_decrypt(UiEvent *event, void *data) { |
|
92 DavConnect2Repo *c2r = event->window; |
|
93 |
|
94 if(event->intval == 4) { |
|
95 char *pw = ui_get(c2r->password); |
|
96 |
|
97 PwdStore *secrets = get_pwdstore(); |
|
98 pwdstore_setpassword(secrets, pw); |
|
99 if(pwdstore_decrypt(secrets)) { |
|
100 ui_dialog(c2r->ui, .title = "Error", .content = "Cannot decrypt Secret Store", .closebutton_label = "OK"); |
|
101 } else { |
|
102 davbrowser_connect2repo(c2r->ui, c2r->browser, c2r->repo, c2r->path); |
|
103 } |
|
104 } |
|
105 free(c2r->path); |
|
106 if(!c2r->repo->node) { |
|
107 dav_repository_free(get_config(), c2r->repo); |
|
108 } |
|
109 |
|
110 ui_close(event->obj); |
|
111 } |
|
112 |
|
113 int davbrowser_connect2repo(UiObject *ui, DavBrowser *browser, DavCfgRepository *repo, const char *path) { |
|
114 char *user = NULL; |
|
115 char *password = NULL; |
|
116 char *password_free = NULL; |
|
117 if (repo->user.value.ptr && repo->password.value.ptr) { |
|
118 user = repo->user.value.ptr; |
|
119 cxmutstr decodedpw = dav_repository_get_decodedpassword(repo); |
|
120 password = decodedpw.ptr; |
|
121 password_free = decodedpw.ptr; |
|
122 } else { |
|
123 PwdStore *secrets = get_pwdstore(); |
|
124 const char *credentials = NULL; |
|
125 if(repo->stored_user.value.ptr) { |
|
126 if(pwdstore_has_id(secrets, repo->stored_user.value.ptr)) { |
|
127 credentials = repo->stored_user.value.ptr; |
|
128 } |
|
129 } else { |
|
130 credentials = get_location_credentials(repo, path); |
|
131 } |
|
132 |
|
133 if(credentials) { |
|
134 if(!secrets->isdecrypted) { |
|
135 UiObject *obj = ui_dialog_window(ui, |
|
136 .title = "Authentication", |
|
137 .lbutton1 = "Cancel", |
|
138 .rbutton4 = "Connect", |
|
139 .default_button = 4, |
|
140 .show_closebutton = UI_OFF, |
|
141 .onclick = dialog_secretstore_decrypt); |
|
142 |
|
143 DavConnect2Repo *c2r = ui_malloc(obj->ctx, sizeof(DavConnect2Repo)); |
|
144 c2r->ui = ui; |
|
145 c2r->browser = browser; |
|
146 c2r->repo = repo; |
|
147 c2r->path = path ? strdup(path) : NULL; |
|
148 c2r->password = ui_string_new(obj->ctx, NULL); |
|
149 obj->window = c2r; |
|
150 |
|
151 ui_grid(obj, .margin = 20, .columnspacing = 12, .rowspacing = 16) { |
|
152 ui_llabel(obj, .label = "Decrypt Secret Store", .colspan = 2); |
|
153 ui_newline(obj); |
|
154 |
|
155 ui_llabel(obj, .label = "Password"); |
|
156 ui_passwordfield(obj, .value = c2r->password, .hexpand = TRUE); |
|
157 } |
|
158 |
|
159 ui_show(obj); |
|
160 |
|
161 return 1; |
|
162 } |
|
163 |
|
164 if(!get_stored_credentials(credentials, &user, &password)) { |
|
165 fprintf(stderr, "Error: failed to get user/password for credentials %s\n", credentials); |
|
166 } |
|
167 } |
|
168 } |
|
169 |
82 DavSession *sn = dav_session_new(application_dav_context(), repo->url.value.ptr); |
170 DavSession *sn = dav_session_new(application_dav_context(), repo->url.value.ptr); |
83 if (repo->user.value.ptr && repo->password.value.ptr) { |
171 if (user && password) { |
84 cxmutstr decodedpw = dav_repository_get_decodedpassword(repo); |
172 dav_session_set_auth(sn, user, password); |
85 dav_session_set_auth(sn, repo->user.value.ptr, decodedpw.ptr); |
173 } |
86 free(decodedpw.ptr); |
174 free(password_free); |
87 } |
175 |
88 browser->sn = sn; |
176 browser->sn = sn; |
89 if (repo->name.value.length > 0) { |
177 if (repo->name.value.length > 0) { |
90 browser->repo_base = cx_strdup(cx_strn(repo->name.value.ptr, repo->name.value.length)).ptr; |
178 browser->repo_base = cx_strdup(cx_strn(repo->name.value.ptr, repo->name.value.length)).ptr; |
91 } else { |
179 } else { |
92 browser->repo_base = cx_strdup(cx_strn(repo->url.value.ptr, repo->url.value.length)).ptr; |
180 browser->repo_base = cx_strdup(cx_strn(repo->url.value.ptr, repo->url.value.length)).ptr; |
103 auth->user = repo->user.value.ptr ? cx_strdup_a(sn->mp->allocator, cx_strcast(repo->user.value)).ptr : NULL; |
191 auth->user = repo->user.value.ptr ? cx_strdup_a(sn->mp->allocator, cx_strcast(repo->user.value)).ptr : NULL; |
104 auth->password = NULL; |
192 auth->password = NULL; |
105 dav_session_set_authcallback(sn, jobthr_davbrowser_auth, auth); |
193 dav_session_set_authcallback(sn, jobthr_davbrowser_auth, auth); |
106 |
194 |
107 davbrowser_query_path(ui, browser, path); |
195 davbrowser_query_path(ui, browser, path); |
|
196 |
|
197 return 0; |
108 } |
198 } |
109 |
199 |
110 // ------------------------------ davbrowser_auth ------------------------------ |
200 // ------------------------------ davbrowser_auth ------------------------------ |
111 |
201 |
112 static int davbrowser_auth_dialog(void *data) { |
202 static int davbrowser_auth_dialog(void *data) { |