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

Commit

Permalink
Token exchange implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg committed Dec 2, 2018
1 parent 6c8abbc commit db4eac7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PhpBox;
use PhpBox\Config\Config;
use PhpBox\Items\{Folder};
use PhpBox\Items\{Item, Folder};

class Box {
private $baseUrl = "https://api.box.com/2.0/";
const baseUrl = "https://api.box.com/2.0/";
private $AccessToken;
private $config;

Expand Down Expand Up @@ -37,6 +37,29 @@ public function requestAccessToken() {
return false;
}

public function requestExchangeToken($scopes = ["base_preview", "item_download"], $folder = NULL, $token = NULL) {
if($token == NULL) {
$token = $this->getValidAccessToken();
}
$client = new \GuzzleHttp\Client();
$params = [
'subject_token' => (string)$token,
'subject_token_type' => 'urn:ietf:params:oauth:token-type:access_token',
'grant_type' => 'urn:ietf:params:oauth:grant-type:token-exchange',
'scope' => implode(" ", $scopes)
];
if ($folder != NULL) {
if ($folder instanceof Item && $folder->isFolder()) {
$folder = (string)($folder->getId());
}
$params['resource'] = Folder::endpointUrl.$folder;
}
$response = $client->request('POST', $this->config->getAuthenticationUrl(), [
'form_params' => $params
]);
return new Token($response->getBody()->getContents());
}

public function getAccessToken() {
return $this->AccessToken;
}
Expand All @@ -55,7 +78,7 @@ public function requestFolder($id = "0", $fields = []) {
if(!empty($fields)) {
$query['fields'] = implode(",", $fields);
}
$response = $client->request('GET', $this->baseUrl."folders/$id", [
$response = $client->request('GET', self::baseUrl."folders/$id", [
'headers' => $headers,
'query' => $query
]);
Expand Down
2 changes: 2 additions & 0 deletions src/Items/Folder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace PhpBox\Items;
use PhpBox\Box;

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

public function __construct($data) {
parent::__construct($data);
Expand Down
8 changes: 8 additions & 0 deletions src/Items/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function __construct($data) {
$this->id = $data->id;
}

public function getId() {
return $this->id;
}

public function getType() {
return $this->type;
}

public function isFolder() {
return $this->type == 'folder';
}
Expand Down

0 comments on commit db4eac7

Please sign in to comment.