MediaWiki  1.33.0
OATHAuthUtils.php
Go to the documentation of this file.
1 <?php
31  public static function isEnabledFor( User $user ) {
32  $oathUser = OATHAuthHooks::getOATHUserRepository()->findByUser( $user );
33  return $oathUser && $oathUser->getKey();
34  }
35 
45  public static function encryptSessionData( array $plaintextVars, $userId ) {
46  $keyMaterial = self::getKeyMaterials();
47  $keys = self::getUserKeys( $keyMaterial, $userId );
48  return self::seal( json_encode( $plaintextVars ), $keys['encrypt'], $keys['hmac'] );
49  }
50 
57  public static function decryptSessionData( $ciphertext, $userId ) {
58  $keyMaterial = self::getKeyMaterials();
59  $keys = self::getUserKeys( $keyMaterial, $userId );
60  return json_decode( self::unseal( $ciphertext, $keys['encrypt'], $keys['hmac'] ), true );
61  }
62 
69  private static function getKeyMaterials() {
70  global $wgOATHAuthSecret, $wgSecretKey;
71  return $wgOATHAuthSecret ?: $wgSecretKey;
72  }
73 
81  private static function getUserKeys( $secret, $userid ) {
82  $keymats = hash_pbkdf2( 'sha256', $secret, "oath-$userid", 10001, 64, true );
83  return [
84  'encrypt' => substr( $keymats, 0, 32 ),
85  'hmac' => substr( $keymats, 32, 32 ),
86  ];
87  }
88 
97  private static function seal( $data, $encKey, $hmacKey ) {
98  $iv = random_bytes( 16 );
99  $ciphertext = openssl_encrypt(
100  $data,
101  'aes-256-ctr',
102  $encKey,
103  OPENSSL_RAW_DATA,
104  $iv
105  );
106  $sealed = base64_encode( $iv ) . '.' . base64_encode( $ciphertext );
107  $hmac = hash_hmac( 'sha256', $sealed, $hmacKey, true );
108  return base64_encode( $hmac ) . '.' . $sealed;
109  }
110 
120  private static function unseal( $encrypted, $encKey, $hmacKey ) {
121  $pieces = explode( '.', $encrypted );
122  if ( count( $pieces ) !== 3 ) {
123  throw new InvalidArgumentException( 'Invalid sealed-secret format' );
124  }
125 
126  list( $hmac, $iv, $ciphertext ) = $pieces;
127  $integCalc = hash_hmac( 'sha256', $iv . '.' . $ciphertext, $hmacKey, true );
128  if ( !hash_equals( $integCalc, base64_decode( $hmac ) ) ) {
129  throw new Exception( 'Sealed secret has been tampered with, aborting.' );
130  }
131 
132  return openssl_decrypt(
133  base64_decode( $ciphertext ),
134  'aes-256-ctr',
135  $encKey,
136  OPENSSL_RAW_DATA,
137  base64_decode( $iv )
138  );
139  }
140 
141 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
captcha-old.count
count
Definition: captcha-old.py:249
OATHAuthUtils\getKeyMaterials
static getKeyMaterials()
Get the base secret for this wiki, used to derive all of the encryption keys.
Definition: OATHAuthUtils.php:69
OATHAuthUtils\unseal
static unseal( $encrypted, $encKey, $hmacKey)
Decrypt data sealed using seal().
Definition: OATHAuthUtils.php:120
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
OATHAuthUtils\encryptSessionData
static encryptSessionData(array $plaintextVars, $userId)
Encrypt an aray of variables to put into the user's session.
Definition: OATHAuthUtils.php:45
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
OATHAuthUtils
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHAuthUtils.php:24
OATHAuthUtils\decryptSessionData
static decryptSessionData( $ciphertext, $userId)
Decrypt an encrypted packet, generated with encryptSessionData.
Definition: OATHAuthUtils.php:57
OATHAuthUtils\isEnabledFor
static isEnabledFor(User $user)
Check whether OATH two-factor authentication is enabled for a given user.
Definition: OATHAuthUtils.php:31
$keys
$keys
Definition: testCompression.php:67
OATHAuthHooks\getOATHUserRepository
static getOATHUserRepository()
Get the singleton OATH user repository.
Definition: OATHAuthHooks.php:34
$wgSecretKey
$wgSecretKey
This should always be customised in LocalSettings.php.
Definition: DefaultSettings.php:5960
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
OATHAuthUtils\seal
static seal( $data, $encKey, $hmacKey)
Actually encrypt the data, using a new random IV, and prepend the hmac of the encrypted data + IV,...
Definition: OATHAuthUtils.php:97
OATHAuthUtils\getUserKeys
static getUserKeys( $secret, $userid)
Generate encryption and hmac keys, unique to this user, based on a single wiki secret.
Definition: OATHAuthUtils.php:81