/
credentials.R
300 lines (271 loc) · 9.52 KB
/
credentials.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#' @title Generate a random password
#' @description Please note that this function is not meant to be used in
#' production. It is not meant to be used for highly secured
#' cryptographic purposes.
#' @param master_password a master password that only you know, should have
#' at least 8 characters
#' @param method whether to query token map, or to create the password,
#' choices are \code{'get_or_create'} (default), \code{'replace'},
#' \code{'query'}; see 'Details'
#' @param service service name, must only contains letters, digits, equal sign,
#' underscore, comma, dot, dash
#' @param special_chr special characters allowed in the password
#' @param tokenfile a file containing all the tokens. Warning: if you lose the
#' token book, it is hard (not impossible, but impractical) to restore
#' the passwords
#' @param verbose whether to print out service names; default is false
#' @return If method is \code{'query'}, returns token map; otherwise returns
#' the password itself
#'
#' @seealso \code{\link[digest]{digest}}
#'
#' @details Please note that this function is not meant to be used in
#' production or anything that requires high security level. This is most
#' likely for my personal use since I am tired of
#' storing the passwords on the cloud or having to buy the services.
#'
#' The encryption adopts \code{'sha256'} algorithm provided by
#' \code{\link[digest]{digest}} function. To restore a password,
#' you will need twp components: \code{master_password}, a token book (
#' \code{tokenfile}). If any of them is missing, then the password is lost.
#' Please store the token book properly (for example, in 'Dropbox' vault).
#'
#' The token book could be shared. Anyone who do not have master password will
#' be unlikely to restore the service password. Do not share the master password
#' with anyone other than yourself.
#'
#' By default, \code{method='get_or_create'} will try to retrieve existing
#' tokens to generate password. If the token is missing, then a new token
#' will be generated. The \code{method='replace'} will ignore existing tokens
#' and directly create a new one.
#'
#' @examples
#'
#' tokenfile <- tempfile()
#'
#' # ---------- Create a password and store the tokens to token book ------
#' pass1 <- get_credential(
#' master_password = "my password",
#' service = "google.com:my_username",
#' special_chr = "@#$%^&*",
#' tokenfile = tokenfile
#' )
#' print(pass1)
#'
#' # ---------- Query existing tokens ------
#' token_params <- get_credential(
#' method = "query",
#' tokenfile = tokenfile,
#' verbose = TRUE
#' )
#'
#' print(token_params)
#'
#' # ---------- retrieve stored password ----------
#' pass2 <- get_credential(
#' master_password = "my password",
#' service = "google.com",
#' tokenfile = tokenfile
#' )
#' identical(pass1, pass2)
#'
#' # Using wrong master password
#' pass3 <- get_credential(
#' master_password = "wrong password",
#' service = "google.com",
#' tokenfile = tokenfile
#' )
#' identical(pass1, pass3)
#'
#' # ---------- Replace token ----------
#' # Existing token will be replaced with a new token
#' pass4 <- get_credential(
#' master_password = "my password",
#' method = "replace",
#' service = "google.com",
#' special_chr = "@#$%^&*",
#' tokenfile = tokenfile
#' )
#' print(pass4)
#' identical(pass1, pass4)
#'
#' @export
get_credential <- function(
master_password, method = c("get_or_create", "replace", "query"),
service = NULL, special_chr = "~`! @#$%^&*()_-+={[}]|:;'<,>.?/",
tokenfile = NULL, verbose = FALSE
){
method <- match.arg(method)
illegal_regexp <- "[^a-zA-Z0-9=_.,@:-]"
if(method %in% c("get_or_create", "replace")) {
# service must be valid
if(length(service) != 1 || is.na(service) || !is.character(service) ||
trimws(service) == "" || grepl(illegal_regexp, service) ) {
stop("In 'get_or_create' or 'replace' mode, `service` must be provided with a non-empty string. This string can only contain letters, digits, and/or the following characters: \"=_.,-\"")
}
if(length(master_password) != 1 || is.na(master_password) ||
nchar(master_password) < 8) {
stop("`master_password` must be a character string of length >= 8")
}
if(length(special_chr) != 1 || is.na(special_chr) || !is.character(special_chr)) {
stop("`special_chr` must be a character string")
}
}
config_dir <- R_user_dir("dipsaus", "config")
default_tokenfile <- file.path(config_dir, "credential_tokenfile")
if(is.null(tokenfile)) {
tokenfile <- getOption("dipsaus.file.credential", default_tokenfile)
on.exit({
if(!file.exists(default_tokenfile)) {
remove_empty_dir(config_dir, recursive = TRUE)
}
})
}
ensure_tokenfile <- function() {
if(file.exists(tokenfile)) { return() }
if(!dir.exists(dirname(tokenfile))) {
dir.create(dirname(tokenfile), showWarnings = FALSE, recursive = TRUE)
}
file.create(tokenfile)
}
# no check, service token seed must contain no space, \t, or \n
encode <- function(service, token, seed, special_chr0) {
if(missing(special_chr0)) {
special_chr0 <- special_chr
} else {
if(length(special_chr0) != 1 || is.na(special_chr0) || !is.character(special_chr0)) {
stop("Internal error: Invalid `special_chr0`")
special_chr0 <- special_chr
}
}
special_enc <- base64_urlencode(special_chr0)
service <- gsub(illegal_regexp, "", service)
if(length(service) != 1 || service == "") { stop("Invalid service") }
service <- base64_urlencode(service)
token <- gsub(illegal_regexp, "", token)
if(length(token) != 1 || service == token) { stop("Invalid token") }
token <- base64_urlencode(token)
seed <- as.integer(seed)
if(length(seed) != 5 || any(is.na(seed) | seed <= 0)) { stop("Invalid seed") }
seed <- base64_urlencode(paste(seed, collapse = " "))
base64_urlencode(sprintf("%s\t%s\t%s\t%s", service, token, seed, special_enc))
}
decode <- function(s) {
s <- base64_urldecode(s)
s <- strsplit(s, split = "\t")[[1]]
service <- base64_urldecode(s[[1]])
token <- base64_urldecode(s[[2]])
seed <- as.integer(strsplit(base64_urldecode(s[[3]]), " ")[[1]])
specials <- base64_urldecode(s[[4]])
if(length(service) != 1 || service == "" ||
grepl(illegal_regexp, service)) { stop("Invalid service") }
if(length(token) != 1 || service == token ||
grepl(illegal_regexp, token)) { stop("Invalid token") }
if(length(seed) != 5 || any(is.na(seed) | seed <= 0)) { stop("Invalid seed") }
if(length(specials) != 1 || !is.character(specials)) {
stop("invalid specials")
}
list(
service = service,
token = token,
seed = seed,
specials = specials
)
}
maps <- NULL
if(file.exists(tokenfile)) {
maps <- lapply(readLines(tokenfile), function(s) {
tryCatch({
return(decode(s))
}, error = function(e){
NULL
})
})
maps <- drop_nulls(maps)
if(length(maps)) {
map_names <- vapply(maps, "[[", FUN.VALUE = "", "service")
dups <- duplicated(map_names)
if(any(dups)) {
map_names <- map_names[!dups]
maps <- maps[!dups]
}
names(maps) <- map_names
}
}
maps <- as.list(maps)
if(verbose) {
cat(names(maps), "", sep = "\n")
}
if(method == "query") {
return(maps)
}
if(service %in% names(maps)) {
if(method == "replace") {
token <- list(
service = service,
token = rand_string(50),
seed = c(sample(2^31-1, 1), sample(20, 4, replace = TRUE)),
specials = special_chr
)
# re-generate map-file
maps[[service]] <- token
enc <- sapply(maps, function(token) {
encode(
service = token$service,
token = token$token,
seed = token$seed,
special_chr0 = token$specials
)
})
ensure_tokenfile()
writeLines(enc, tokenfile)
} else {
token <- maps[[service]]
}
} else {
token <- list(
service = service,
token = rand_string(50),
seed = c(sample(2^31-1, 1), sample(20, 4, replace = TRUE)),
specials = special_chr
)
enc <- encode(
service = token$service,
token = token$token,
seed = token$seed,
special_chr0 = token$specials
)
# Append
ensure_tokenfile()
cat(sprintf("%s\n", enc), file = tokenfile, append = TRUE)
}
pwd_sig <- digest::digest(master_password, algo = "sha256", serialize = FALSE)
token_string <- sprintf("MasterPassword:%s Service:%s Token:%s Seed: %s", pwd_sig, token$service, token$token, paste(token$seed, collapse = ","))
# generate password
pwd <- digest::digest(token_string, algo = "sha256", serialize = FALSE)
# break pwd every 4 characters
pwd <- apply(matrix(strsplit(pwd, "")[[1]], nrow = 2), 2, function(x){
strtoi(paste(x, collapse = ""), base = 16L)
})
pwd_order <- order(pwd)
distribution <- sort(token$seed[-1])
distribution <- ceiling(distribution / sum(distribution) * length(pwd))
distribution <- cumsum(distribution)
pwd_case <- 5 - (
(pwd_order <= distribution[[1]]) + (pwd_order <= distribution[[2]]) +
(pwd_order <= distribution[[3]]) + (pwd_order <= distribution[[4]])
)
dict_special <- strsplit(token$specials, "")[[1]]
dict_digit <- as.character(seq.int(0,9))
dict_lower <- letters
dict_upper <- letters
dict_list <- list(
dict_special, dict_digit, dict_lower, dict_upper
)
res <- apply(cbind(pwd, pwd_case), 1, function(x){
dict <- dict_list[[x[[2]]]]
dict[(x[[1]] %% (length(dict) - 1)) + 1]
})
res <- paste(res, collapse = "")
return(res)
}