4 * This file is (c) Copyright 2010 by Olivier BERGER, Madhumita DHAR, Institut TELECOM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * This program has been developed in the frame of the COCLICO
21 * project with financial support of its funders.
25 // OAuth PHP library (http://code.google.com/p/oauth/source/browse/#svn%2Fcode%2Fphp) as of rev. 622
27 require_once 'OAuth.php';
32 * This is only the base class that will be subclassed by Request Tokens or Access Tokens
33 * All tokens have an ID in the DB, and are issued for a consumer, at a certain time (stamp)
34 * When a token has been authorized by a user, the user id is recorded
36 * @author Olivier Berger
39 class OauthAuthzToken extends OAuthToken {
41 protected $id; // in the table
43 protected $consumer_id; // consumer for which the token was issued
44 protected $user_id; // user for which the token is (or will be) authorized
45 protected $time_stamp; // time of creation of the token
47 const TOKEN_TYPE = 'null';
50 * @param int $p_consumer_id
51 * @param string $p_key
52 * @param string $p_secret
53 * @param int $p_user_id
54 * @param int $p_time_stamp
56 function __construct( $p_consumer_id, $p_key, $p_secret, $p_user_id=null, $p_time_stamp=null) {
57 // parent only stores key and secret
58 parent::__construct($p_key, $p_secret);
60 // will be set once inserted in the DB
63 $this->consumer_id = $p_consumer_id;
64 $this->user_id = $p_user_id;
65 $this->time_stamp = $p_time_stamp;
68 public function getId() {
72 public function getConsumerId() {
73 return $this->consumer_id;
76 public function getUserId() {
77 return $this->user_id;
80 public function gettime_stamp() {
81 return $this->time_stamp;
85 static function row_to_new_token ($t_row) {
86 $t_token = new OauthAuthzToken( $t_row['consumer_id'], $t_row['token_key'], $t_row['token_secret'] );
87 $t_token->id = $t_row['id'];
93 * Loads a particular token from the DB knowing its ID
97 static function load( $p_id, $token_type ) {
99 $DBSTORE = FFDbOAuthDataStore::singleton();
101 $t_row = $DBSTORE->find_token_from_id($token_type, $p_id);
104 exit_error( "Error trying to load token!", 'oauthprovider' );
110 * @param int $user_id
111 * @return Ambigous <multitype:, unknown>
113 static function load_all($user_id=null, $token_type) {
115 $DBSTORE = FFDbOAuthDataStore::singleton();
116 $t_rows = $DBSTORE->find_all_tokens($token_type, $user_id);
121 * Loads a token by its token key
123 * @param string $p_token_key
124 * @return OauthAuthzToken subclass
126 static function load_by_key( $p_token_key, $token_type ) {
128 $DBSTORE = FFDbOAuthDataStore::singleton();
130 $t_row = $DBSTORE->find_token_from_key($token_type, $p_token_key);
133 exit_error( "Error trying to load ".$token_type." token!", 'oauthprovider' );
139 * Check that mandatory values are OK
141 function check_mandatory() {
142 if ( strlen(trim( $this->consumer_id ))==0 || strlen(trim( $this->key ))==0 || strlen(trim( $this->secret ))==0 ) {
143 throw new OAuthException('Mandatory "consumer_id", "key" or "secret" Token attribute(s) not set.');
150 function delete($token_type) {
152 $DBSTORE = FFDbOAuthDataStore::singleton();
154 $DBSTORE->delete_token( $token_type, $this->id);