remove old readme
[atutor.git] / docs / mods / _standard / social / lib / BasicSecurityToken.php
1 <?php
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 /**
22  * Primitive token implementation that uses stings as tokens.
23  */
24 class BasicSecurityToken extends SecurityToken {
25   /** serialized form of the token */
26   private $token;
27   
28   /** data from the token */
29   private $tokenData;
30   
31   /** tool to use for signing and encrypting the token */
32   protected $crypter;
33   
34   private $OWNER_KEY = "o";
35   private $APP_KEY = "a";
36   private $VIEWER_KEY = "v";
37   private $DOMAIN_KEY = "d";
38   private $APPURL_KEY = "u";
39   private $MODULE_KEY = "m";
40
41   /**
42    * {@inheritDoc}
43    */
44   public function toSerialForm() {
45     return urlencode($this->token);
46   }
47
48   /**
49    * Generates a token from an input string
50    * @param token String form of token
51    * @param maxAge max age of the token (in seconds)
52    * @throws BlobCrypterException 
53    */
54   static public function createFromToken($token, $maxAge) {
55     return new BasicSecurityToken($token, $maxAge, null, null, null, null, null, null);
56   }
57
58   /**
59    * Generates a token from an input array of values
60    * @param owner owner of this gadget
61    * @param viewer viewer of this gadget
62    * @param app application id
63    * @param domain domain of the container
64    * @param appUrl url where the application lives
65    * @param moduleId module id of this gadget 
66    * @throws BlobCrypterException 
67    */
68   static public function createFromValues($owner, $viewer, $app, $domain, $appUrl, $moduleId) {
69     return new BasicSecurityToken(null, null, $owner, $viewer, $app, $domain, $appUrl, $moduleId);
70   }
71
72   public function __construct($token, $maxAge, $owner, $viewer, $app, $domain, $appUrl, $moduleId) {
73     $this->crypter = $this->getCrypter();
74     if (! empty($token)) {
75       $this->token = $token;
76       $this->tokenData = $this->crypter->unwrap($token, $maxAge);
77     } else {
78       $this->tokenData = array();
79       $this->tokenData[$this->OWNER_KEY] = $owner;
80       $this->tokenData[$this->VIEWER_KEY] = $viewer;
81       $this->tokenData[$this->APP_KEY] = $app;
82       $this->tokenData[$this->DOMAIN_KEY] = $domain;
83       $this->tokenData[$this->APPURL_KEY] = $appUrl;
84       $this->tokenData[$this->MODULE_KEY] = $moduleId;
85       $this->token = $this->crypter->wrap($this->tokenData);
86     }
87 //      debug($this->tokenData);
88   }
89
90   protected function getCrypter() {
91     return new BasicBlobCrypter();
92   }
93
94   public function isAnonymous() {
95     return ($this->tokenData[$this->OWNER_KEY] === 0 && $this->tokenData[$this->VIEWER_KEY] === 0);
96   }
97
98   /**
99    * {@inheritDoc}
100    */
101   public function getAppId() {
102     if ($this->isAnonymous()) {
103       throw new Exception("Can't get appId from an anonymous token");
104     }
105     return $this->tokenData[$this->APP_KEY];
106   }
107
108   /**
109    * {@inheritDoc}
110    */
111   public function getDomain() {
112     if ($this->isAnonymous()) {
113       throw new Exception("Can't get domain from an anonymous token");
114     }
115     return $this->tokenData[$this->DOMAIN_KEY];
116   }
117
118   /**
119    * {@inheritDoc}
120    */
121   public function getOwnerId() {
122     if ($this->isAnonymous()) {
123       throw new Exception("Can't get ownerId from an anonymous token");
124     }
125     return $this->tokenData[$this->OWNER_KEY];
126   }
127
128   /**
129    * {@inheritDoc}
130    */
131   public function getViewerId() {
132     if ($this->isAnonymous()) {
133       throw new Exception("Can't get viewerId from an anonymous token");
134     }
135     return $this->tokenData[$this->VIEWER_KEY];
136   }
137
138   /**
139    * {@inheritDoc}
140    */
141   public function getAppUrl() {
142     if ($this->isAnonymous()) {
143       throw new Exception("Can't get appUrl from an anonymous token");
144     }
145     return $this->tokenData[$this->APPURL_KEY];
146   }
147
148   /**
149    * {@inheritDoc}
150    */
151   public function getModuleId() {
152     if ($this->isAnonymous()) {
153       throw new Exception("Can't get moduleId from an anonymous token");
154     }
155     if (! is_numeric($this->tokenData[$this->MODULE_KEY])) {
156       throw new Exception("Module ID should be an integer");
157     }
158     return $this->tokenData[$this->MODULE_KEY];
159   }
160 }