Completed
Push — master ( 5213c5...c28fe2 )
by Lukas
38:38 queued 26:14
created
settings/Controller/AuthSettingsController.php 1 patch
Indentation   +160 added lines, -160 removed lines patch added patch discarded remove patch
@@ -38,165 +38,165 @@
 block discarded – undo
38 38
 
39 39
 class AuthSettingsController extends Controller {
40 40
 
41
-	/** @var IProvider */
42
-	private $tokenProvider;
43
-
44
-	/** @var IUserManager */
45
-	private $userManager;
46
-
47
-	/** @var ISession */
48
-	private $session;
49
-
50
-	/** @var string */
51
-	private $uid;
52
-
53
-	/** @var ISecureRandom */
54
-	private $random;
55
-
56
-	/**
57
-	 * @param string $appName
58
-	 * @param IRequest $request
59
-	 * @param IProvider $tokenProvider
60
-	 * @param IUserManager $userManager
61
-	 * @param ISession $session
62
-	 * @param ISecureRandom $random
63
-	 * @param string $userId
64
-	 */
65
-	public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager,
66
-		ISession $session, ISecureRandom $random, $userId) {
67
-		parent::__construct($appName, $request);
68
-		$this->tokenProvider = $tokenProvider;
69
-		$this->userManager = $userManager;
70
-		$this->uid = $userId;
71
-		$this->session = $session;
72
-		$this->random = $random;
73
-	}
74
-
75
-	/**
76
-	 * @NoAdminRequired
77
-	 * @NoSubadminRequired
78
-	 *
79
-	 * @return JSONResponse
80
-	 */
81
-	public function index() {
82
-		$user = $this->userManager->get($this->uid);
83
-		if (is_null($user)) {
84
-			return [];
85
-		}
86
-		$tokens = $this->tokenProvider->getTokenByUser($user);
41
+    /** @var IProvider */
42
+    private $tokenProvider;
43
+
44
+    /** @var IUserManager */
45
+    private $userManager;
46
+
47
+    /** @var ISession */
48
+    private $session;
49
+
50
+    /** @var string */
51
+    private $uid;
52
+
53
+    /** @var ISecureRandom */
54
+    private $random;
55
+
56
+    /**
57
+     * @param string $appName
58
+     * @param IRequest $request
59
+     * @param IProvider $tokenProvider
60
+     * @param IUserManager $userManager
61
+     * @param ISession $session
62
+     * @param ISecureRandom $random
63
+     * @param string $userId
64
+     */
65
+    public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager,
66
+        ISession $session, ISecureRandom $random, $userId) {
67
+        parent::__construct($appName, $request);
68
+        $this->tokenProvider = $tokenProvider;
69
+        $this->userManager = $userManager;
70
+        $this->uid = $userId;
71
+        $this->session = $session;
72
+        $this->random = $random;
73
+    }
74
+
75
+    /**
76
+     * @NoAdminRequired
77
+     * @NoSubadminRequired
78
+     *
79
+     * @return JSONResponse
80
+     */
81
+    public function index() {
82
+        $user = $this->userManager->get($this->uid);
83
+        if (is_null($user)) {
84
+            return [];
85
+        }
86
+        $tokens = $this->tokenProvider->getTokenByUser($user);
87 87
 		
88
-		try {
89
-			$sessionId = $this->session->getId();
90
-		} catch (SessionNotAvailableException $ex) {
91
-			return $this->getServiceNotAvailableResponse();
92
-		}
93
-		try {
94
-			$sessionToken = $this->tokenProvider->getToken($sessionId);
95
-		} catch (InvalidTokenException $ex) {
96
-			return $this->getServiceNotAvailableResponse();
97
-		}
98
-
99
-		return array_map(function(IToken $token) use ($sessionToken) {
100
-			$data = $token->jsonSerialize();
101
-			if ($sessionToken->getId() === $token->getId()) {
102
-				$data['canDelete'] = false;
103
-				$data['current'] = true;
104
-			} else {
105
-				$data['canDelete'] = true;
106
-			}
107
-			return $data;
108
-		}, $tokens);
109
-	}
110
-
111
-	/**
112
-	 * @NoAdminRequired
113
-	 * @NoSubadminRequired
114
-	 * @PasswordConfirmationRequired
115
-	 *
116
-	 * @param string $name
117
-	 * @return JSONResponse
118
-	 */
119
-	public function create($name) {
120
-		try {
121
-			$sessionId = $this->session->getId();
122
-		} catch (SessionNotAvailableException $ex) {
123
-			return $this->getServiceNotAvailableResponse();
124
-		}
125
-
126
-		try {
127
-			$sessionToken = $this->tokenProvider->getToken($sessionId);
128
-			$loginName = $sessionToken->getLoginName();
129
-			try {
130
-				$password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
131
-			} catch (PasswordlessTokenException $ex) {
132
-				$password = null;
133
-			}
134
-		} catch (InvalidTokenException $ex) {
135
-			return $this->getServiceNotAvailableResponse();
136
-		}
137
-
138
-		$token = $this->generateRandomDeviceToken();
139
-		$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
140
-		$tokenData = $deviceToken->jsonSerialize();
141
-		$tokenData['canDelete'] = true;
142
-
143
-		return new JSONResponse([
144
-			'token' => $token,
145
-			'loginName' => $loginName,
146
-			'deviceToken' => $tokenData,
147
-		]);
148
-	}
149
-
150
-	private function getServiceNotAvailableResponse() {
151
-		$resp = new JSONResponse();
152
-		$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
153
-		return $resp;
154
-	}
155
-
156
-	/**
157
-	 * Return a 25 digit device password
158
-	 *
159
-	 * Example: AbCdE-fGhIj-KlMnO-pQrSt-12345
160
-	 *
161
-	 * @return string
162
-	 */
163
-	private function generateRandomDeviceToken() {
164
-		$groups = [];
165
-		for ($i = 0; $i < 5; $i++) {
166
-			$groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE);
167
-		}
168
-		return implode('-', $groups);
169
-	}
170
-
171
-	/**
172
-	 * @NoAdminRequired
173
-	 * @NoSubadminRequired
174
-	 *
175
-	 * @return JSONResponse
176
-	 */
177
-	public function destroy($id) {
178
-		$user = $this->userManager->get($this->uid);
179
-		if (is_null($user)) {
180
-			return [];
181
-		}
182
-
183
-		$this->tokenProvider->invalidateTokenById($user, $id);
184
-		return [];
185
-	}
186
-
187
-	/**
188
-	 * @NoAdminRequired
189
-	 * @NoSubadminRequired
190
-	 *
191
-	 * @param int $id
192
-	 * @param array $scope
193
-	 */
194
-	public function update($id, array $scope) {
195
-		$token = $this->tokenProvider->getTokenById($id);
196
-		$token->setScope([
197
-			'filesystem' => $scope['filesystem']
198
-		]);
199
-		$this->tokenProvider->updateToken($token);
200
-		return [];
201
-	}
88
+        try {
89
+            $sessionId = $this->session->getId();
90
+        } catch (SessionNotAvailableException $ex) {
91
+            return $this->getServiceNotAvailableResponse();
92
+        }
93
+        try {
94
+            $sessionToken = $this->tokenProvider->getToken($sessionId);
95
+        } catch (InvalidTokenException $ex) {
96
+            return $this->getServiceNotAvailableResponse();
97
+        }
98
+
99
+        return array_map(function(IToken $token) use ($sessionToken) {
100
+            $data = $token->jsonSerialize();
101
+            if ($sessionToken->getId() === $token->getId()) {
102
+                $data['canDelete'] = false;
103
+                $data['current'] = true;
104
+            } else {
105
+                $data['canDelete'] = true;
106
+            }
107
+            return $data;
108
+        }, $tokens);
109
+    }
110
+
111
+    /**
112
+     * @NoAdminRequired
113
+     * @NoSubadminRequired
114
+     * @PasswordConfirmationRequired
115
+     *
116
+     * @param string $name
117
+     * @return JSONResponse
118
+     */
119
+    public function create($name) {
120
+        try {
121
+            $sessionId = $this->session->getId();
122
+        } catch (SessionNotAvailableException $ex) {
123
+            return $this->getServiceNotAvailableResponse();
124
+        }
125
+
126
+        try {
127
+            $sessionToken = $this->tokenProvider->getToken($sessionId);
128
+            $loginName = $sessionToken->getLoginName();
129
+            try {
130
+                $password = $this->tokenProvider->getPassword($sessionToken, $sessionId);
131
+            } catch (PasswordlessTokenException $ex) {
132
+                $password = null;
133
+            }
134
+        } catch (InvalidTokenException $ex) {
135
+            return $this->getServiceNotAvailableResponse();
136
+        }
137
+
138
+        $token = $this->generateRandomDeviceToken();
139
+        $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
140
+        $tokenData = $deviceToken->jsonSerialize();
141
+        $tokenData['canDelete'] = true;
142
+
143
+        return new JSONResponse([
144
+            'token' => $token,
145
+            'loginName' => $loginName,
146
+            'deviceToken' => $tokenData,
147
+        ]);
148
+    }
149
+
150
+    private function getServiceNotAvailableResponse() {
151
+        $resp = new JSONResponse();
152
+        $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE);
153
+        return $resp;
154
+    }
155
+
156
+    /**
157
+     * Return a 25 digit device password
158
+     *
159
+     * Example: AbCdE-fGhIj-KlMnO-pQrSt-12345
160
+     *
161
+     * @return string
162
+     */
163
+    private function generateRandomDeviceToken() {
164
+        $groups = [];
165
+        for ($i = 0; $i < 5; $i++) {
166
+            $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE);
167
+        }
168
+        return implode('-', $groups);
169
+    }
170
+
171
+    /**
172
+     * @NoAdminRequired
173
+     * @NoSubadminRequired
174
+     *
175
+     * @return JSONResponse
176
+     */
177
+    public function destroy($id) {
178
+        $user = $this->userManager->get($this->uid);
179
+        if (is_null($user)) {
180
+            return [];
181
+        }
182
+
183
+        $this->tokenProvider->invalidateTokenById($user, $id);
184
+        return [];
185
+    }
186
+
187
+    /**
188
+     * @NoAdminRequired
189
+     * @NoSubadminRequired
190
+     *
191
+     * @param int $id
192
+     * @param array $scope
193
+     */
194
+    public function update($id, array $scope) {
195
+        $token = $this->tokenProvider->getTokenById($id);
196
+        $token->setScope([
197
+            'filesystem' => $scope['filesystem']
198
+        ]);
199
+        $this->tokenProvider->updateToken($token);
200
+        return [];
201
+    }
202 202
 }
Please login to merge, or discard this patch.
lib/public/Security/ISecureRandom.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -36,56 +36,56 @@
 block discarded – undo
36 36
  */
37 37
 interface ISecureRandom {
38 38
 
39
-	/**
40
-	 * Flags for characters that can be used for <code>generate($length, $characters)</code>
41
-	 */
42
-	const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
43
-	const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
44
-	const CHAR_DIGITS = '0123456789';
45
-	const CHAR_SYMBOLS = '!\"#$%&\\\'()* +,-./:;<=>?@[\]^_`{|}~';
39
+    /**
40
+     * Flags for characters that can be used for <code>generate($length, $characters)</code>
41
+     */
42
+    const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
43
+    const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
44
+    const CHAR_DIGITS = '0123456789';
45
+    const CHAR_SYMBOLS = '!\"#$%&\\\'()* +,-./:;<=>?@[\]^_`{|}~';
46 46
 
47
-	/**
48
-	 * Characters that can be used for <code>generate($length, $characters)</code>, to
49
-	 * generate human readable random strings. Lower- and upper-case characters and digits 
50
-	 * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
51
-	 */
52
-	const CHAR_HUMAN_READABLE = "abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789";
47
+    /**
48
+     * Characters that can be used for <code>generate($length, $characters)</code>, to
49
+     * generate human readable random strings. Lower- and upper-case characters and digits 
50
+     * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
51
+     */
52
+    const CHAR_HUMAN_READABLE = "abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789";
53 53
 
54
-	/**
55
-	 * Convenience method to get a low strength random number generator.
56
-	 *
57
-	 * Low Strength should be used anywhere that random strings are needed
58
-	 * in a non-cryptographical setting. They are not strong enough to be
59
-	 * used as keys or salts. They are however useful for one-time use tokens.
60
-	 *
61
-	 * @return $this
62
-	 * @since 8.0.0
63
-	 * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
64
-	 */
65
-	public function getLowStrengthGenerator();
54
+    /**
55
+     * Convenience method to get a low strength random number generator.
56
+     *
57
+     * Low Strength should be used anywhere that random strings are needed
58
+     * in a non-cryptographical setting. They are not strong enough to be
59
+     * used as keys or salts. They are however useful for one-time use tokens.
60
+     *
61
+     * @return $this
62
+     * @since 8.0.0
63
+     * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
64
+     */
65
+    public function getLowStrengthGenerator();
66 66
 
67
-	/**
68
-	 * Convenience method to get a medium strength random number generator.
69
-	 *
70
-	 * Medium Strength should be used for most needs of a cryptographic nature.
71
-	 * They are strong enough to be used as keys and salts. However, they do
72
-	 * take some time and resources to generate, so they should not be over-used
73
-	 *
74
-	 * @return $this
75
-	 * @since 8.0.0
76
-	 * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
77
-	 */
78
-	public function getMediumStrengthGenerator();
67
+    /**
68
+     * Convenience method to get a medium strength random number generator.
69
+     *
70
+     * Medium Strength should be used for most needs of a cryptographic nature.
71
+     * They are strong enough to be used as keys and salts. However, they do
72
+     * take some time and resources to generate, so they should not be over-used
73
+     *
74
+     * @return $this
75
+     * @since 8.0.0
76
+     * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
77
+     */
78
+    public function getMediumStrengthGenerator();
79 79
 
80
-	/**
81
-	 * Generate a random string of specified length.
82
-	 * @param int $length The length of the generated string
83
-	 * @param string $characters An optional list of characters to use if no character list is
84
-	 * 							specified all valid base64 characters are used.
85
-	 * @return string
86
-	 * @since 8.0.0
87
-	 */
88
-	public function generate($length,
89
-							 $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
80
+    /**
81
+     * Generate a random string of specified length.
82
+     * @param int $length The length of the generated string
83
+     * @param string $characters An optional list of characters to use if no character list is
84
+     * 							specified all valid base64 characters are used.
85
+     * @return string
86
+     * @since 8.0.0
87
+     */
88
+    public function generate($length,
89
+                                $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
90 90
 
91 91
 }
Please login to merge, or discard this patch.