Browse Source

Support for JWT Tokens.

Shane Mc Cormack 6 years ago
parent
commit
59c7525150
1 changed files with 26 additions and 1 deletions
  1. 26 1
      src/MyDNSHostAPI.php

+ 26 - 1
src/MyDNSHostAPI.php

@@ -116,6 +116,17 @@
 			return $this;
 			return $this;
 		}
 		}
 
 
+		/**
+		 * Auth using JWT Token.
+		 *
+		 * @param $token Token to auth with
+		 * @return $this for chaining.
+		 */
+		public function setAuthJWT($token) {
+			$this->auth = ['type' => 'jwt', 'token' => $token];
+			return $this;
+		}
+
 		/**
 		/**
 		 * Auth using a custom auth method.
 		 * Auth using a custom auth method.
 		 *
 		 *
@@ -592,6 +603,18 @@
 			return $this->api('/users/' . $userid . '/customdata/' . $key, 'DELETE');
 			return $this->api('/users/' . $userid . '/customdata/' . $key, 'DELETE');
 		}
 		}
 
 
+		/**
+		 * Get a JWT Token from the backend
+		 *
+		 * @return Backend JWT Token or null if we are not authed.
+		 */
+		public function getJWTToken() {
+			if ($this->auth === FALSE) { return NULL; }
+
+			$result = $this->api('/session/jwt');
+			return isset($result['response']['token']) ? $result['response']['token'] : NULL;
+		}
+
 		/**
 		/**
 		 * Get a session ID from the backend
 		 * Get a session ID from the backend
 		 *
 		 *
@@ -1105,7 +1128,9 @@
 			if ($auth == NULL) { $auth = $this->auth; }
 			if ($auth == NULL) { $auth = $this->auth; }
 
 
 			if ($auth !== FALSE) {
 			if ($auth !== FALSE) {
-				if ($auth['type'] == 'session') {
+				if ($auth['type'] == 'jwt') {
+					$headers['Authorization'] = 'Bearer ' . $auth['token'];
+				} else if ($auth['type'] == 'session') {
 					$headers['X-SESSION-ID'] = $auth['sessionid'];
 					$headers['X-SESSION-ID'] = $auth['sessionid'];
 				} else if ($auth['type'] == 'userkey') {
 				} else if ($auth['type'] == 'userkey') {
 					$headers['X-API-USER'] = $auth['user'];
 					$headers['X-API-USER'] = $auth['user'];