Skip to content

Commit

Permalink
Access refactored to enabled more types (#3053)
Browse files Browse the repository at this point in the history
* Access refactored to enabled more types

* minor fixes
  • Loading branch information
dvoytenko committed May 3, 2016
1 parent ba23705 commit 5028d67
Show file tree
Hide file tree
Showing 7 changed files with 809 additions and 416 deletions.
7 changes: 4 additions & 3 deletions examples/article-access.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<title>Lorem Ipsum | PublisherName</title>
<link rel="canonical" href="amps.html" >
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<!-- See https://github.com/ampproject/amp-publisher-sample -->
<script id="amp-access" type="application/json">
{
"authorization": "http://localhost:8002/amp-authorization.json?rid=READER_ID&url=CANONICAL_URL&_=RANDOM",
"pingback": "http://localhost:8002/amp-pingback?rid=READER_ID&url=CANONICAL_URL&views=AUTHDATA(views)",
"login": "http://localhost:8002/amp-login?rid=READER_ID&url=CANONICAL_URL",
"authorization": "http://localhost:8002/amp-access/api/amp-authorization.json?rid=READER_ID&url=CANONICAL_URL&ref=DOCUMENT_REFERRER&_=RANDOM",
"pingback": "http://localhost:8002/amp-access/api/amp-pingback?rid=READER_ID&ref=DOCUMENT_REFERRER&url=CANONICAL_URL",
"login": "http://localhost:8002/amp-access/login/?rid=READER_ID&url=CANONICAL_URL",
"authorizationFallbackResponse": {"error": true}
}
</script>
Expand Down
107 changes: 107 additions & 0 deletions extensions/amp-access/0.1/amp-access-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {assertHttpsUrl} from '../../../src/url';
import {dev, user} from '../../../src/log';
import {timer} from '../../../src/timer';
import {xhrFor} from '../../../src/xhr';

/** @const {string} */
const TAG = 'amp-access-client';

/** @const {number} */
const AUTHORIZATION_TIMEOUT = 3000;


/** @implements {AccessTypeAdapterDef} */
export class AccessClientAdapter {

/**
* @param {!Window} win
* @param {!JSONObject} configJson
* @param {!AccessTypeAdapterContextDef} context
*/
constructor(win, configJson, context) {
/** @const {!Window} */
this.win = win;

/** @const @private {!AccessTypeAdapterContextDef} */
this.context_ = context;

/** @const @private {string} */
this.authorizationUrl_ = user.assert(configJson['authorization'],
'"authorization" URL must be specified');
assertHttpsUrl(this.authorizationUrl_, '"authorization"');

/** @const @private {string} */
this.pingbackUrl_ = user.assert(configJson['pingback'],
'"pingback" URL must be specified');
assertHttpsUrl(this.pingbackUrl_, '"pingback"');

/** @const @private {!Xhr} */
this.xhr_ = xhrFor(win);

/** @const @private {!Timer} */
this.timer_ = timer;
}

/** @override */
getConfig() {
return {
'authorizationUrl': this.authorizationUrl_,
'pingbackUrl': this.pingbackUrl_,
};
}

/** @override */
isAuthorizationEnabled() {
return true;
}

/** @override */
authorize() {
dev.fine(TAG, 'Start authorization via ', this.authorizationUrl_);
const urlPromise = this.context_.buildUrl(this.authorizationUrl_,
/* useAuthData */ false);
return urlPromise.then(url => {
dev.fine(TAG, 'Authorization URL: ', url);
return this.timer_.timeoutPromise(
AUTHORIZATION_TIMEOUT,
this.xhr_.fetchJson(url, {
credentials: 'include',
requireAmpResponseSourceOrigin: true,
}));
});
}

/** @override */
pingback() {
const promise = this.context_.buildUrl(this.pingbackUrl_,
/* useAuthData */ true);
return promise.then(url => {
dev.fine(TAG, 'Pingback URL: ', url);
return this.xhr_.sendSignal(url, {
method: 'POST',
credentials: 'include',
requireAmpResponseSourceOrigin: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: '',
});
});
}
}
74 changes: 74 additions & 0 deletions extensions/amp-access/0.1/amp-access-other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {dev} from '../../../src/log';
import {isProxyOrigin} from '../../../src/url';

/** @const {string} */
const TAG = 'amp-access-other';


/** @implements {AccessTypeAdapterDef} */
export class AccessOtherAdapter {

/**
* @param {!Window} win
* @param {!JSONObject} configJson
* @param {!AccessTypeAdapterContextDef} context
*/
constructor(win, configJson, context) {
/** @const {!Window} */
this.win = win;

/** @const @private {!AccessTypeAdapterContextDef} */
this.context_ = context;

/** @private {?JSONObject} */
this.authorizationResponse_ =
configJson['authorizationFallbackResponse'] || null;

/** @const @private {boolean} */
this.isProxyOrigin_ = isProxyOrigin(win.location);
}

/** @override */
getConfig() {
return {
'authorizationResponse': this.authorizationResponse_,
};
}

/** @override */
isAuthorizationEnabled() {
// The `type=other` is allowed to use the authorization fallback, but
// only if it's not on `cdn.ampproject.org`.
return (!!this.authorizationResponse_ && !this.isProxyOrigin_);
}

/** @override */
authorize() {
dev.fine(TAG, 'Use the authorization fallback for type=other');
// Only allowed for proxy origin (`cdn.ampproject.org`).
dev.assert(!this.isProxyOrigin_);
return Promise.resolve(dev.assert(this.authorizationResponse_));
}

/** @override */
pingback() {
dev.fine(TAG, 'Ignore pingback');
return Promise.resolve();
}
}
Loading

0 comments on commit 5028d67

Please sign in to comment.