MediaWiki  1.33.0
OATHUserRepository.php
Go to the documentation of this file.
1 <?php
19 use Psr\Log\LoggerInterface;
22 
25  protected $lb;
26 
28  protected $cache;
29 
31  private $logger;
32 
39  $this->lb = $lb;
40  $this->cache = $cache;
41 
42  $this->setLogger( \MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' ) );
43  }
44 
48  public function setLogger( LoggerInterface $logger ) {
49  $this->logger = $logger;
50  }
51 
56  public function findByUser( User $user ) {
57  $oathUser = $this->cache->get( $user->getName() );
58  if ( !$oathUser ) {
59  $oathUser = new OATHUser( $user, null );
60 
61  $uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
62  $res = $this->getDB( DB_REPLICA )->selectRow(
63  'oathauth_users',
64  '*',
65  [ 'id' => $uid ],
66  __METHOD__
67  );
68  if ( $res ) {
69  $key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
70  $oathUser->setKey( $key );
71  }
72 
73  $this->cache->set( $user->getName(), $oathUser );
74  }
75  return $oathUser;
76  }
77 
82  public function persist( OATHUser $user, $clientInfo ) {
83  $prevUser = $this->findByUser( $user->getUser() );
84 
85  $this->getDB( DB_MASTER )->replace(
86  'oathauth_users',
87  [ 'id' ],
88  [
89  'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ),
90  'secret' => $user->getKey()->getSecret(),
91  'scratch_tokens' => implode( ',', $user->getKey()->getScratchTokens() ),
92  ],
93  __METHOD__
94  );
95 
96  $userName = $user->getUser()->getName();
97  $this->cache->set( $userName, $user );
98 
99  if ( $prevUser !== false ) {
100  $this->logger->info( 'OATHAuth updated for {user} from {clientip}', [
101  'user' => $userName,
102  'clientip' => $clientInfo,
103  ] );
104  } else {
105  // If findByUser() has returned false, there was no user row or cache entry
106  $this->logger->info( 'OATHAuth enabled for {user} from {clientip}', [
107  'user' => $userName,
108  'clientip' => $clientInfo,
109  ] );
110  }
111  }
112 
117  public function remove( OATHUser $user, $clientInfo ) {
118  $this->getDB( DB_MASTER )->delete(
119  'oathauth_users',
120  [ 'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ) ],
121  __METHOD__
122  );
123 
124  $userName = $user->getUser()->getName();
125  $this->cache->delete( $userName );
126 
127  $this->logger->info( 'OATHAuth disabled for {user} from {clientip}', [
128  'user' => $userName,
129  'clientip' => $clientInfo,
130  ] );
131  }
132 
137  private function getDB( $index ) {
138  global $wgOATHAuthDatabase;
139 
140  return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
141  }
142 }
OATHUserRepository\$cache
BagOStuff $cache
Definition: OATHUserRepository.php:28
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
OATHUserRepository
Definition: OATHUserRepository.php:23
OATHUserRepository\__construct
__construct(ILoadBalancer $lb, BagOStuff $cache)
OATHUserRepository constructor.
Definition: OATHUserRepository.php:38
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:58
$res
$res
Definition: database.txt:21
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
OATHUserRepository\getDB
getDB( $index)
Definition: OATHUserRepository.php:137
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
OATHAuthKey
Class representing a two-factor key.
Definition: OATHAuthKey.php:29
MediaWiki
A helper class for throttling authentication attempts.
OATHUser
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHUser.php:24
OATHUserRepository\persist
persist(OATHUser $user, $clientInfo)
Definition: OATHUserRepository.php:82
OATHUserRepository\findByUser
findByUser(User $user)
Definition: OATHUserRepository.php:56
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
OATHUser\getUser
getUser()
Definition: OATHUser.php:44
OATHUserRepository\$lb
ILoadBalancer $lb
Definition: OATHUserRepository.php:25
OATHUserRepository\setLogger
setLogger(LoggerInterface $logger)
Definition: OATHUserRepository.php:48
Wikimedia\Rdbms\DBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: DBConnRef.php:14
OATHUserRepository\$logger
LoggerInterface $logger
Definition: OATHUserRepository.php:31
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2452
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:78