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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
22 * Primitive token implementation that uses stings as tokens.
24 class BasicSecurityToken extends SecurityToken {
25 /** serialized form of the token */
28 /** data from the token */
31 /** tool to use for signing and encrypting the token */
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";
44 public function toSerialForm() {
45 return urlencode($this->token);
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
54 static public function createFromToken($token, $maxAge) {
55 return new BasicSecurityToken($token, $maxAge, null, null, null, null, null, null);
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
68 static public function createFromValues($owner, $viewer, $app, $domain, $appUrl, $moduleId) {
69 return new BasicSecurityToken(null, null, $owner, $viewer, $app, $domain, $appUrl, $moduleId);
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);
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);
87 // debug($this->tokenData);
90 protected function getCrypter() {
91 return new BasicBlobCrypter();
94 public function isAnonymous() {
95 return ($this->tokenData[$this->OWNER_KEY] === 0 && $this->tokenData[$this->VIEWER_KEY] === 0);
101 public function getAppId() {
102 if ($this->isAnonymous()) {
103 throw new Exception("Can't get appId from an anonymous token");
105 return $this->tokenData[$this->APP_KEY];
111 public function getDomain() {
112 if ($this->isAnonymous()) {
113 throw new Exception("Can't get domain from an anonymous token");
115 return $this->tokenData[$this->DOMAIN_KEY];
121 public function getOwnerId() {
122 if ($this->isAnonymous()) {
123 throw new Exception("Can't get ownerId from an anonymous token");
125 return $this->tokenData[$this->OWNER_KEY];
131 public function getViewerId() {
132 if ($this->isAnonymous()) {
133 throw new Exception("Can't get viewerId from an anonymous token");
135 return $this->tokenData[$this->VIEWER_KEY];
141 public function getAppUrl() {
142 if ($this->isAnonymous()) {
143 throw new Exception("Can't get appUrl from an anonymous token");
145 return $this->tokenData[$this->APPURL_KEY];
151 public function getModuleId() {
152 if ($this->isAnonymous()) {
153 throw new Exception("Can't get moduleId from an anonymous token");
155 if (! is_numeric($this->tokenData[$this->MODULE_KEY])) {
156 throw new Exception("Module ID should be an integer");
158 return $this->tokenData[$this->MODULE_KEY];