Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit db4eac7

Browse files
committed
Token exchange implemented.
1 parent 6c8abbc commit db4eac7

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Box.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace PhpBox;
44
use PhpBox\Config\Config;
5-
use PhpBox\Items\{Folder};
5+
use PhpBox\Items\{Item, Folder};
66

77
class Box {
8-
private $baseUrl = "https://api.box.com/2.0/";
8+
const baseUrl = "https://api.box.com/2.0/";
99
private $AccessToken;
1010
private $config;
1111

@@ -37,6 +37,29 @@ public function requestAccessToken() {
3737
return false;
3838
}
3939

40+
public function requestExchangeToken($scopes = ["base_preview", "item_download"], $folder = NULL, $token = NULL) {
41+
if($token == NULL) {
42+
$token = $this->getValidAccessToken();
43+
}
44+
$client = new \GuzzleHttp\Client();
45+
$params = [
46+
'subject_token' => (string)$token,
47+
'subject_token_type' => 'urn:ietf:params:oauth:token-type:access_token',
48+
'grant_type' => 'urn:ietf:params:oauth:grant-type:token-exchange',
49+
'scope' => implode(" ", $scopes)
50+
];
51+
if ($folder != NULL) {
52+
if ($folder instanceof Item && $folder->isFolder()) {
53+
$folder = (string)($folder->getId());
54+
}
55+
$params['resource'] = Folder::endpointUrl.$folder;
56+
}
57+
$response = $client->request('POST', $this->config->getAuthenticationUrl(), [
58+
'form_params' => $params
59+
]);
60+
return new Token($response->getBody()->getContents());
61+
}
62+
4063
public function getAccessToken() {
4164
return $this->AccessToken;
4265
}
@@ -55,7 +78,7 @@ public function requestFolder($id = "0", $fields = []) {
5578
if(!empty($fields)) {
5679
$query['fields'] = implode(",", $fields);
5780
}
58-
$response = $client->request('GET', $this->baseUrl."folders/$id", [
81+
$response = $client->request('GET', self::baseUrl."folders/$id", [
5982
'headers' => $headers,
6083
'query' => $query
6184
]);

src/Items/Folder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

33
namespace PhpBox\Items;
4+
use PhpBox\Box;
45

56
class Folder extends Item {
67
protected $contains = [];
8+
const endpointUrl = Box::baseUrl.'folders/';
79

810
public function __construct($data) {
911
parent::__construct($data);

src/Items/Item.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ public function __construct($data) {
1313
$this->id = $data->id;
1414
}
1515

16+
public function getId() {
17+
return $this->id;
18+
}
19+
20+
public function getType() {
21+
return $this->type;
22+
}
23+
1624
public function isFolder() {
1725
return $this->type == 'folder';
1826
}

0 commit comments

Comments
 (0)