forked from sciserver/SciScript-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthentication.r
106 lines (91 loc) · 3.68 KB
/
Authentication.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
#require(httr)
#require(jsonlite)
#source('Config.r')
# this variable stores the Authentication token for the user's session
Authentication.token = NULL
# returns a dictionary with name/id for the user corresponding to the specified token
Authentication.getKeystoneUserWithToken<-function(token){
taskName = ""
if(Config.isSciServerComputeEnvironment()){
taskName = "Compute.SciScript-R.Authentication.getKeystoneUserWithToken"
}else{
taskName = "SciScript-R.Authentication.getKeystoneUserWithToken"
}
loginURL = paste(Config.AuthenticationURL,"/",token,"?TaskName=",taskName,sep='')
#r=GET(loginURL,encode="json",accept("text/plain"),content_type_json(),add_headers('X-Auth-Token'=token))
r=GET(loginURL,encode="json",content_type_json(),add_headers('X-Auth-Token'=token))
if(r$status_code != 200) {
stop(paste("Http Response returned status code ", r$status_code, ":\n", content(r, as="text", encoding="UTF-8")))
} else {
r= content(r)
user={}
user$userName=r$token$user$name
user$id=r$token$user$id
return(user)
}
}
# login and return token which is then also set as the Sys environment variable "sciservertoken" and the variable Authentication.token
Authentication.login<-function(UserName, Password){
taskName = ""
if(Config.isSciServerComputeEnvironment()){
taskName = "Compute.SciScript-R.Authentication.login"
}else{
taskName = "SciScript-R.Authentication.login"
}
loginURL = paste(Config.AuthenticationURL,"?TaskName=",taskName,sep="")
authJson = list(auth=list(identity=list(password=list(user=list(name=unbox(UserName),password=unbox(Password))))))
r=POST(loginURL ,encode="json",body=authJson,accept("text/plain"),content_type_json())
if(r$status_code != 200) {
stop(paste("Http Response returned status code ", r$status_code, ":\n", content(r, as="text", encoding="UTF-8")))
} else {
token=headers(r)$`x-subject-token`
Authentication.setToken(token)
return(token)
}
}
Authentication.getToken<-function(){
if(Config.isSciServerComputeEnvironment() == TRUE){
f = Config.KeystoneTokenFilePath
if(file.exists(f)){
token = readLines(f)
if (!is.null(token) && token != ""){
unlockBinding("Authentication.token", as.environment("package:SciServer"))
assign("Authentication.token",token,envir=as.environment("package:SciServer"))
Sys.unsetenv("sciservertoken")
Sys.setenv(sciservertoken=token)
return(token)
}else{
warning(paste("In Authentication.getToken: Cannot find token in system token file ",Config.KeystoneTokenFilePath, sep=""))
return(NULL)
}
}else{
warning(paste("In Authentication.getToken: Cannot find system token file ",Config.KeystoneTokenFilePath,sep=""))
return(NULL)
}
}else{
token <- NULL
if(exists("Authentication.token")){
token <- Authentication.token
}
if (is.null(token)) {
token = Sys.getenv("sciservertoken")
if (!is.null(token) && token != ""){
Authentication.setToken(token)
return(token)
}else{
warning("In Authentication.getToken: Authentication token is not defined: the user did not log in with the Authentication.login function")
return(NULL)
}
}
}
}
Authentication.setToken<-function(token){
if( Config.isSciServerComputeEnvironment() == TRUE){
warning("Authentication token cannot be set to arbitary value when inside SciServer-Compute environment.")
}else{
unlockBinding("Authentication.token", as.environment("package:SciServer"))
assign("Authentication.token",token,envir=as.environment("package:SciServer"))
Sys.unsetenv("sciservertoken")
Sys.setenv(sciservertoken=token)
}
}