4 # FILE: SPT--SPTUser.php
6 # Part of the Collection Workflow Integration System (CWIS)
7 # Copyright 2004-2013 Edward Almasy and Internet Scout Research Group
8 # http://scout.wisc.edu/cwis/
13 # ---- PUBLIC INTERFACE --------------------------------------------------
18 # call parent constructor
19 $this->
User($UserInfo);
35 # Clear all keys more than two days old
36 $DB->Query(
"DELETE FROM LoginKeys WHERE NOW() - CreationTime > 172800");
37 $DB->Query(
"DELETE FROM UsedLoginTokens WHERE NOW()-KeyCTime > 172800");
39 # Get the most recently generated key
40 $DB->Query(
"SELECT NOW()-CreationTime as Age,"
41 .
"KeyPair FROM LoginKeys "
42 .
"ORDER BY Age ASC LIMIT 1");
43 $Row =
$DB->FetchRow();
45 # If there is no key in the database, or the key is too old
46 if ( ($Row===FALSE) || ($Row[
"Age"]>=86400) )
48 # Generate a new OpenSSL format keypair
49 $KeyPair = openssl_pkey_new(
51 'private_key_bits' => 512, # Make
this a Sysadmin pref later?
52 'private_key_type' => OPENSSL_KEYTYPE_RSA
55 # Serialize it for storage
56 openssl_pkey_export($KeyPair, $KeyPairDBFormat);
58 # And stick it into the database
59 $DB->Query(
"INSERT INTO LoginKeys "
60 .
"(KeyPair, CreationTime) VALUES ("
61 .
"\"".addslashes($KeyPairDBFormat).
"\","
66 # If we do have a current key in the database,
67 # Convert it to openssl format for usage
68 $KeyPair = openssl_pkey_get_private( $Row[
"KeyPair"] );
82 # Export the keypair as an ASCII signing request (which contains the data we want)
83 openssl_csr_export(openssl_csr_new(array(), $KeyPair), $Export, FALSE);
89 '/Modulus \([0-9]+ bit\):(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
90 '/Public-Key: \([0-9]+ bit\).*Modulus:(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
93 foreach ($Patterns as $Pattern)
95 if (preg_match($Pattern, $Export, $Matches))
97 $Modulus = $Matches[1];
98 $Exponent = $Matches[2];
103 # Clean newlines and whitespace out of the modulus
104 $Modulus = preg_replace(
"/[^0-9a-f]/",
"", $Modulus);
106 # Return key material
107 return array(
"Modulus" => $Modulus,
"Exponent" => $Exponent );
SQL database abstraction object with smart query caching.
static ExtractPubKeyParameters($KeyPair)
Extract the modulus and exponent of the public key from an OpenSSL format keypair to send in login fo...
User($UserInfoOne=NULL, $UserInfoTwo=NULL)
static GetCryptKey()
Get/generate a cryptographic keypair for user login.
CWIS-specific user class.