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 require_once 'token_api.php';
28 * Request Token concrete class
30 * Extends the OauthAuthzToken to add the boolean if the request token is authorized and pending
31 * consumption by the consumer.
33 * @author Olivier Berger
36 class OauthAuthzRequestToken extends OauthAuthzToken {
38 protected $authorized; // if a user has authorized the token
39 protected $verifier; //the oauth verifier code
40 protected $role_id; //access level granted
42 const TOKEN_TYPE = 'request';
45 * @param int $p_consumer_id
46 * @param string $p_key
47 * @param string $p_secret
48 * @param bool $p_authorized
49 * @param int $p_user_id
50 * @param int $p_time_stamp
52 function __construct( $p_consumer_id, $p_key, $p_secret, $p_authorized=false, $p_verifier=FALSE, $p_user_id=null, $p_role_id=null, $p_time_stamp=null) {
53 parent::__construct( $p_consumer_id, $p_key, $p_secret, $p_user_id, $p_time_stamp);
55 $this->authorized = $p_authorized;
56 $this->verifier = $p_verifier;
57 $this->role_id = $p_role_id;
60 public function getAuthorized() {
61 return $this->authorized;
64 public function getVerifier() {
65 return $this->verifier;
68 public function getRoleId() {
69 return $this->role_id;
73 * Converts a row returned by select * into an object
75 * @return OauthAuthzRequestToken
77 static function row_to_new_token ($t_row) {
78 $t_token = new OauthAuthzRequestToken( $t_row['consumer_id'], $t_row['token_key'], $t_row['token_secret'], $t_row['authorized'], $t_row['verifier'], $t_row['user_id'], $t_row['role_id'], $t_row['time_stamp'] );
80 $t_token->id = $t_row['id'];
85 static function load( $p_id ) {
86 $row = parent::load($p_id, self::TOKEN_TYPE);
87 return self::row_to_new_token($row);
90 static function load_all($user_id=null) {
91 $rows = parent::load_all($user_id=null, self::TOKEN_TYPE);
94 foreach ($rows as $row) {
95 $token = self::row_to_new_token($row);
103 static function load_by_key( $p_token_key ) {
104 $row = parent::load_by_key($p_token_key, self::TOKEN_TYPE);
105 return self::row_to_new_token($row);
109 parent::delete(self::TOKEN_TYPE);
113 * Saves the token properly to the DB (insert or update with proper columns)
117 $this->check_mandatory();
119 if ( $this->authorized && strlen(trim( $this->user_id ))==0 ) {
120 exit_error( "Error trying to save request token!", 'oauthprovider' );
123 $DBSTORE = FFDbOAuthDataStore::singleton();
124 $this->id = $DBSTORE->save_request_token($this);
127 public function authorize($user_id, $role_id) {
128 $this->authorized = 1;
129 $this->verifier = substr(sha1(util_randbytes(32)),-10,12);
130 $this->user_id = $user_id;
131 $this->role_id = $role_id;
133 return $this->verifier;
136 public function check_verifier($verifier) {
137 if(!$this->verifier || ($this->verifier!=$verifier)) {
138 throw new OAuthException("Incorrect OAuth verification code provided.");