MediaWiki REL1_33
OATHUserRepository.php
Go to the documentation of this file.
1<?php
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
Class representing a two-factor key.
persist(OATHUser $user, $clientInfo)
setLogger(LoggerInterface $logger)
LoggerInterface $logger
__construct(ILoadBalancer $lb, BagOStuff $cache)
OATHUserRepository constructor.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition OATHUser.php:24
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2452
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition DBConnRef.php:14
$res
Definition database.txt:21
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
Database cluster connection, tracking, load balancing, and transaction manager interface.
you have access to all of the normal MediaWiki so you can get a DB use the cache
A helper class for throttling authentication attempts.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26