2023-02-08 15:26:23 +00:00
when ( NimMajor , NimMinor ) < ( 1 , 4 ) :
{. push raises : [ Defect ] . }
else :
{. push raises : [ ] . }
2024-03-15 23:08:47 +00:00
import options , json , strutils , sequtils , std / [ tables , os ]
2023-02-08 15:26:23 +00:00
2024-03-15 23:08:47 +00:00
import . / keyfile , . / conversion_utils , . / protocol_types , . / utils
2023-02-08 15:26:23 +00:00
2024-03-15 23:08:47 +00:00
# This proc creates an empty keystore (i.e. with no credentials)
proc createAppKeystore * (
path : string , appInfo : AppInfo , separator : string = " \n "
) : KeystoreResult [ void ] =
let keystore = AppKeystore (
application : appInfo . application ,
appIdentifier : appInfo . appIdentifier ,
version : appInfo . version ,
credentials : initTable [ string , KeystoreMembership ] ( ) ,
)
2023-02-08 15:26:23 +00:00
var jsonKeystore : string
jsonKeystore . toUgly ( % keystore )
var f : File
if not f . open ( path , fmWrite ) :
2024-03-15 23:08:47 +00:00
return
err ( AppKeystoreError ( kind : KeystoreOsError , msg : " Cannot open file for writing " ) )
2023-02-08 15:26:23 +00:00
try :
# To avoid other users/attackers to be able to read keyfiles, we make the file readable/writable only by the running user
setFilePermissions ( path , { fpUserWrite , fpUserRead } )
f . write ( jsonKeystore )
# We separate keystores with separator
f . write ( separator )
ok ( )
except CatchableError :
2024-03-15 23:08:47 +00:00
err ( AppKeystoreError ( kind : KeystoreOsError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
finally :
f . close ( )
2023-04-04 13:34:53 +00:00
# This proc load a keystore based on the application, appIdentifier and version filters.
2023-02-08 15:26:23 +00:00
# If none is found, it automatically creates an empty keystore for the passed parameters
2024-03-15 23:08:47 +00:00
proc loadAppKeystore * (
path : string , appInfo : AppInfo , separator : string = " \n "
) : KeystoreResult [ JsonNode ] =
2023-02-08 15:26:23 +00:00
## Load and decode JSON keystore from pathname
var data : JsonNode
var matchingAppKeystore : JsonNode
# If no keystore exists at path we create a new empty one with passed keystore parameters
if fileExists ( path ) = = false :
2023-08-29 12:16:21 +00:00
let newKeystoreRes = createAppKeystore ( path , appInfo , separator )
if newKeystoreRes . isErr ( ) :
return err ( newKeystoreRes . error )
2023-02-08 15:26:23 +00:00
try :
# We read all the file contents
var f : File
if not f . open ( path , fmRead ) :
2024-03-15 23:08:47 +00:00
return err (
AppKeystoreError ( kind : KeystoreOsError , msg : " Cannot open file for reading " )
)
2023-02-08 15:26:23 +00:00
let fileContents = readAll ( f )
# We iterate over each substring split by separator (which we expect to correspond to a single keystore json)
for keystore in fileContents . split ( separator ) :
# We skip if read line is empty
if keystore . len = = 0 :
continue
# We skip all lines that don't seem to define a json
if not keystore . startsWith ( " { " ) or not keystore . endsWith ( " } " ) :
continue
try :
# We parse the json
data = json . parseJson ( keystore )
2023-04-04 13:34:53 +00:00
# We check if parsed json contains the relevant keystore credentials fields and if these are set to the passed parameters
2023-02-08 15:26:23 +00:00
# (note that "if" is lazy, so if one of the .contains() fails, the json fields contents will not be checked and no ResultDefect will be raised due to accessing unavailable fields)
if data . hasKeys ( [ " application " , " appIdentifier " , " credentials " , " version " ] ) and
2024-03-15 23:08:47 +00:00
data [ " application " ] . getStr ( ) = = appInfo . application and
data [ " appIdentifier " ] . getStr ( ) = = appInfo . appIdentifier and
data [ " version " ] . getStr ( ) = = appInfo . version :
2023-02-08 15:26:23 +00:00
# We return the first json keystore that matches the passed app parameters
# We assume a unique kesytore with such parameters is present in the file
matchingAppKeystore = data
break
# TODO: we might continue rather than return for some of these errors
except JsonParsingError :
2024-03-15 23:08:47 +00:00
return
err ( AppKeystoreError ( kind : KeystoreJsonError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
except ValueError :
2024-03-15 23:08:47 +00:00
return
err ( AppKeystoreError ( kind : KeystoreJsonError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
except OSError :
2024-03-15 23:08:47 +00:00
return
err ( AppKeystoreError ( kind : KeystoreOsError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
except Exception : #parseJson raises Exception
2024-03-15 23:08:47 +00:00
return
err ( AppKeystoreError ( kind : KeystoreOsError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
except IOError :
2024-03-15 23:08:47 +00:00
return err ( AppKeystoreError ( kind : KeystoreIoError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
return ok ( matchingAppKeystore )
2023-08-29 12:16:21 +00:00
# Adds a membership credential to the keystore matching the application, appIdentifier and version filters.
2024-03-15 23:08:47 +00:00
proc addMembershipCredentials * (
path : string ,
membership : KeystoreMembership ,
password : string ,
appInfo : AppInfo ,
separator : string = " \n " ,
) : KeystoreResult [ void ] =
2023-02-08 15:26:23 +00:00
# We load the keystore corresponding to the desired parameters
# This call ensures that JSON has all required fields
let jsonKeystoreRes = loadAppKeystore ( path , appInfo , separator )
if jsonKeystoreRes . isErr ( ) :
2023-08-29 12:16:21 +00:00
return err ( jsonKeystoreRes . error )
2023-02-08 15:26:23 +00:00
# We load the JSON node corresponding to the app keystore
var jsonKeystore = jsonKeystoreRes . get ( )
try :
if jsonKeystore . hasKey ( " credentials " ) :
# We get all credentials in keystore
2023-08-29 12:16:21 +00:00
let keystoreCredentials = jsonKeystore [ " credentials " ]
let key = membership . hash ( )
if keystoreCredentials . hasKey ( key ) :
# noop
return ok ( )
2023-04-04 13:34:53 +00:00
2023-08-29 12:16:21 +00:00
let encodedMembershipCredential = membership . encode ( )
let keyfileRes = createKeyFileJson ( encodedMembershipCredential , password )
if keyfileRes . isErr ( ) :
2024-03-15 23:08:47 +00:00
return err (
AppKeystoreError ( kind : KeystoreCreateKeyfileError , msg : $ keyfileRes . error )
)
2023-02-08 15:26:23 +00:00
2023-08-29 12:16:21 +00:00
# We add it to the credentials field of the keystore
jsonKeystore [ " credentials " ] [ key ] = keyfileRes . get ( )
2023-04-04 13:34:53 +00:00
except CatchableError :
2024-03-15 23:08:47 +00:00
return err ( AppKeystoreError ( kind : KeystoreJsonError , msg : getCurrentExceptionMsg ( ) ) )
2023-02-08 15:26:23 +00:00
# We save to disk the (updated) keystore.
2023-08-29 12:16:21 +00:00
let saveRes = save ( jsonKeystore , path , separator )
if saveRes . isErr ( ) :
return err ( saveRes . error )
2023-04-04 13:34:53 +00:00
2023-02-08 15:26:23 +00:00
return ok ( )
# Returns the membership credentials in the keystore matching the application, appIdentifier and version filters, further filtered by the input
# identity credentials and membership contracts
2024-03-15 23:08:47 +00:00
proc getMembershipCredentials * (
path : string , password : string , query : KeystoreMembership , appInfo : AppInfo
) : KeystoreResult [ KeystoreMembership ] =
2023-02-08 15:26:23 +00:00
# We load the keystore corresponding to the desired parameters
# This call ensures that JSON has all required fields
let jsonKeystoreRes = loadAppKeystore ( path , appInfo )
if jsonKeystoreRes . isErr ( ) :
2023-08-29 12:16:21 +00:00
return err ( jsonKeystoreRes . error )
2023-02-08 15:26:23 +00:00
# We load the JSON node corresponding to the app keystore
var jsonKeystore = jsonKeystoreRes . get ( )
try :
if jsonKeystore . hasKey ( " credentials " ) :
# We get all credentials in keystore
var keystoreCredentials = jsonKeystore [ " credentials " ]
2023-09-04 10:16:44 +00:00
if keystoreCredentials . len = = 0 :
2023-08-29 12:16:21 +00:00
# error
2024-03-15 23:08:47 +00:00
return err (
AppKeystoreError (
kind : KeystoreCredentialNotFoundError ,
msg : " No credentials found in keystore " ,
)
)
2023-09-04 10:16:44 +00:00
var keystoreCredential : JsonNode
if keystoreCredentials . len = = 1 :
2024-03-15 23:08:47 +00:00
keystoreCredential = keystoreCredentials . getFields ( ) . values ( ) . toSeq ( ) [ 0 ]
2023-09-04 10:16:44 +00:00
else :
let key = query . hash ( )
if not keystoreCredentials . hasKey ( key ) :
# error
2024-03-15 23:08:47 +00:00
return err (
AppKeystoreError (
kind : KeystoreCredentialNotFoundError ,
msg : " Credential not found in keystore " ,
)
)
2023-09-04 10:16:44 +00:00
keystoreCredential = keystoreCredentials [ key ]
2023-08-29 12:16:21 +00:00
let decodedKeyfileRes = decodeKeyFileJson ( keystoreCredential , password )
if decodedKeyfileRes . isErr ( ) :
2024-03-15 23:08:47 +00:00
return err (
AppKeystoreError (
kind : KeystoreReadKeyfileError , msg : $ decodedKeyfileRes . error
)
)
2023-08-29 12:16:21 +00:00
# we parse the json decrypted keystoreCredential
let decodedCredentialRes = decode ( decodedKeyfileRes . get ( ) )
let keyfileMembershipCredential = decodedCredentialRes . get ( )
return ok ( keyfileMembershipCredential )
2023-04-04 13:34:53 +00:00
except CatchableError :
2024-03-15 23:08:47 +00:00
return err ( AppKeystoreError ( kind : KeystoreJsonError , msg : getCurrentExceptionMsg ( ) ) )