-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.R
236 lines (205 loc) · 7.2 KB
/
server.R
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Define server logic required
#==========================================================================================
server <- function(input, output, session) {
auth_info = reactiveVal(NULL)
session_reloaded = reactiveVal(FALSE)
# Read the session cookie
cookie_observer = observe({
req(!session_reloaded())
session_cookie <- cookies::get_cookie("user_session")
if (!is.null(session_cookie)) {
INFO("Found a session cookie - reload dashboard")
session_data <- jsonlite::fromJSON(session_cookie)
if(any(sapply(appConfig$auth_endpoints, function(x){x$auth_url == session_data$endpoint$auth_url}))){
endpoint = appConfig$auth_endpoints[sapply(appConfig$auth_endpoints, function(x){x$auth_url == session_data$endpoint$auth_url})][[1]]
switch(endpoint$auth_type,
"ocs" = {
#try to reconnect behind the scene
AUTH_API = try(ocs4R::ocsManager$new(
url = endpoint$auth_url,
user = session_data$user,
pwd = session_data$password
))
if(!is(AUTH_API, "try-error")) if(is(AUTH_API, "ocsManager") && !is.null(AUTH_API$getWebdavRoot())) {
assign("AUTH_API", AUTH_API, envir = GEOFLOW_SHINY_ENV)
shinyjs::show("main-content")
shinyjs::hide("login-wrapper")
auth_info(session_data)
}
}
)
}
cookie_observer$destroy()
}
})
onStop(function(){
resetAuthSessionVariables(session)
})
if(appConfig$auth){
if(appConfig$auth_ui){
#auth with UI
credentials <- authLoginServer(
id = "login",
config = appConfig,
log_out = reactive(logout_init())
)
observe({
if (credentials()$user_auth) auth_info(credentials()$auth_info)
})
# call the logout module with reactive trigger to hide/show
logout_init <- shinyauthr::logoutServer(
id = "logout",
active = reactive(!is.null(auth_info()))
)
observe({
if (!is.null(auth_info())) {
if(!is.null(auth_info()) & !session_reloaded()){
print(auth_info())
initAuthSessionVariables(session, auth_info())
INFO("Load home module")
home_server("home", auth_info, parent.session = session)
INFO("Load configuration editor module")
config_editor_server("config_editor", auth_info, parent.session = session)
INFO("Load configuration list module")
config_list_server("config_list", auth_info, parent.session = session)
}
shinyjs::show("main-content")
shinyjs::hide("login-wrapper")
session_reloaded(TRUE)
session_data = list(
endpoint = auth_info()$endpoint,
user = auth_info()$user,
password = auth_info()$password
)
print(session_data)
cookies::set_cookie("user_session", as.character(jsonlite::toJSON(session_data)), expiration = 7)
} else {
shinyjs::hide("main-content")
shinyjs::show("login-wrapper")
}
})
observeEvent(input$`logout-button`, {
logout_init()
shinyjs::hide("main-content")
shinyjs::show("login-wrapper")
cookies::remove_cookie("user_session")
})
}else{
#auth without UI
shinyjs::show("main-content")
shinyjs::hide("login-wrapper")
#JWT auth
observe({
jwt_profile <- decodeJWT(appConfig$auth_jwt)
#D4Science JWT Auth - setting interaction with D4Science StorageHub
if(startsWith(jwt_profile$iss, "https://accounts.d4science.org")){
AUTH_API <- try(d4storagehub4R::StoragehubManager$new(token = jwt_profile$access$access_token, token_type = "jwt"))
if (is(AUTH_API, "StoragehubManager")) {
assign("AUTH_API", AUTH_API, envir = GEOFLOW_SHINY_ENV)
auth_info(list(
endpoint = list(auth_url = "https://api.d4science.org/workspace", auth_type = "d4science"),
backend = NA,
service = NA,
user = AUTH_API$getUserProfile()$username,
token = jwt_profile$access$access_token
))
if(!is.null(auth_info())){
INFO("Set-up geoflow-shiny in auth mode (no UI, token based)")
initAuthSessionVariables(session, auth_info())
INFO("Load home module")
home_server("home", auth_info, parent.session = session)
INFO("Load configuration editor module")
config_editor_server("config_editor", auth_info, parent.session = session)
INFO("Load configuration list module")
config_list_server("config_list", auth_info, parent.session = session)
}
}
}
})
}
}else{
#anonymous usage
INFO("Set-up geoflow-shiny in anonymous mode")
INFO("Load home module")
home_server("home", parent.session = session)
INFO("Load configuration editor module")
config_editor_server("config_editor", parent.session = session)
INFO("Load configuration list module")
config_list_server("config_list", parent.session = session)
shinyjs::show("main-content")
shinyjs::hide("login-wrapper")
}
renderSideUI = function(){
bs4Dash::sidebarMenu(
id = "geoflow-tabs",
bs4Dash::menuItem(
text = "Home",
tabName = "home",
selected = TRUE
),
bs4Dash::menuItem(
text = "Configure",
tabName = "config",
bs4Dash::menuSubItem(text = "Edit configuration", tabName = "config_editor"),
startExpanded = TRUE
),
bs4Dash::menuItem(
text = "Execute",
tabName = "exec",
bs4Dash::menuSubItem(text = "Execute workflow", tabName = "config_list"),
startExpanded = TRUE
)
)
}
output$side_ui <- renderUI({
if(appConfig$auth){
if(appConfig$auth_ui){
req(!is.null(auth_info()))
renderSideUI()
}else{
renderSideUI()
}
}else{
renderSideUI()
}
})
renderMainUI = function(){
tabItems(
home_ui("home"),
config_editor_ui("config_editor"),
config_list_ui("config_list")
)
}
output$main_ui <- renderUI({
if(appConfig$auth){
if(appConfig$auth_ui){
req(!is.null(auth_info()))
renderMainUI()
}else{
renderMainUI()
}
}else{
renderMainUI()
}
})
output$user_avatar <- renderUI({
tags$a(
href = "#",
class = "nav-link",
style = "float:right;",
tags$img(
src = sprintf("%s/avatar/%s/64", auth_info()$endpoint$auth_url, auth_info()$user),
class = "img-circle",
style = "height: 32px; width: 32px; margin-top: -10px;"
)
)
})
#module page management
session$userData$module <- reactiveVal(NULL)
observe({
#Update moduleUrl
moduleUrl <- gsub('_', '-', input$`geoflow-tabs`)
session$userData$module(moduleUrl)
updateModuleUrl(session, moduleUrl)
})
}