1 <?xml version="1.0" encoding="utf-8"?>
3 xmlns:mx="http://www.adobe.com/2006/mxml"
5 horizontalAlign="center" title="Authorisation Required"
6 creationComplete="getRequestToken()"
9 <mx:ViewStack id="contentStack" width="100%" height="100%">
11 <mx:VBox id="okPanel" width="100%" height="100%">
12 <mx:Text width="100%" text="{getAuthText()}"/>
13 <mx:VBox width="100%" id="gotLinkBox" visible="false">
14 <mx:Text width="100%">
16 Click the link below to open a web page where
17 you will be asked to authorise access to this app.
20 <mx:LinkButton id="link"
21 label="http://oauth.dev.openstreetmap.org/oauth/authorize?somekey"
22 click="openURL(authoriseURL); tryAccessButton.enabled=true;"/>
23 <mx:Text width="100%">
24 <mx:text>Once you've authorised the access click the 'Try Access' button below</mx:text>
26 <mx:Text styleName="failText" visible="false" id="deniedLabel">
27 <mx:text><![CDATA[<b>Access was denied, please check, and try again</b>]]></mx:text>
32 <mx:VBox id="permFailPanel" width="100%" height="100%">
33 <mx:Text styleName="failText" width="100%" condenseWhite="true">
34 <mx:htmlText><![CDATA[
35 <p>The server refused this application's credentials -- an authorisation link
36 could not be obtained.
38 <b>OAuth access will not be possible.</b>
40 Please contact application vendor to find out what's going on.
45 <mx:VBox id="tempFailPanel" width="100%" height="100%">
46 <mx:Text width="100%">
48 There was a problem contacting the server to get authorisation.
49 This may be a temporary error, try again later.
56 <mx:ControlBar horizontalAlign="right">
58 <mx:ProgressBar id="progress" label="Contacting server..." labelPlacement="top"
59 indeterminate="true"/>
60 <mx:Spacer width="100%"/>
62 <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);"/>
63 <mx:Button id="tryAccessButton" label="Try Access" click="getAccessToken()" enabled="false"/>
67 import flash.events.Event;
69 import mx.managers.PopUpManager;
70 import net.systemeD.halcyon.connection.*;
71 import org.iotashan.oauth.*;
73 private var connection:Connection;
74 private var requestToken:OAuthToken;
75 private var _accessToken:OAuthToken;
76 private var authoriseURL:String;
77 private var lastHTTPStatus:int = 0;
79 public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
81 private function getAuthText():String {
82 return "To save data you must authorise this application to edit "+
83 Connection.serverName + " on your behalf.";
86 private function openURL(url:String):void {
87 var urlRequest:URLRequest = new URLRequest(url);
88 navigateToURL(urlRequest, "_blank");
91 private function getRequestToken():void {
92 connection = Connection.getConnectionInstance();
94 var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
95 var consumer:OAuthConsumer = getConsumer();
96 var url:String = Connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
98 var params:Object = new Object();
99 var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, params, consumer, null);
100 var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
102 // build the actual request
103 var urlReq:URLRequest = new URLRequest(String(urlStr));
104 var loader:URLLoader = new URLLoader();
105 loader.addEventListener(Event.COMPLETE, loadedRequestToken);
106 loader.addEventListener(IOErrorEvent.IO_ERROR, requestTokenError);
107 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
111 private function recordStatus(event:HTTPStatusEvent):void {
112 lastHTTPStatus = event.status;
115 private function requestTokenError(event:IOErrorEvent):void {
116 trace("error occured... last status was: "+lastHTTPStatus);
118 if ( lastHTTPStatus == 401 ) {
119 // this means authorisation was refused -- refused at this stage
120 // means our consumer token is broken
121 contentStack.selectedChild = permFailPanel;
123 contentStack.selectedChild = tempFailPanel;
125 progress.visible = false;
128 private function loadedRequestToken(event:Event):void {
129 trace("Yay! response: "+URLLoader(event.target).data);
130 requestToken = getResponseToken(URLLoader(event.target));
132 var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");
134 authoriseURL = url + "?oauth_token="+requestToken.key;
135 progress.visible = false;
136 gotLinkBox.visible = true;
139 private function getResponseToken(loader:URLLoader):OAuthToken {
140 var vars:URLVariables = new URLVariables(loader.data);
142 // build out request token
143 var token:OAuthToken = new OAuthToken(
144 String(vars["oauth_token"]),
145 String(vars["oauth_token_secret"]));
149 private function getAccessToken():void {
150 var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
151 var consumer:OAuthConsumer = getConsumer();
152 var url:String = Connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
154 var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
155 var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
157 var urlReq:URLRequest = new URLRequest(String(urlStr));
158 var loader:URLLoader = new URLLoader();
159 loader.addEventListener(Event.COMPLETE, loadedAccessToken);
160 loader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenError);
161 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
164 progress.label = "Checking access";
165 progress.visible = true;
168 private function loadedAccessToken(event:Event):void {
169 trace("Yay! response: "+URLLoader(event.target).data);
170 progress.label = "Received Access";
171 progress.indeterminate = false;
172 progress.setProgress(100,100);
173 PopUpManager.removePopUp(this);
175 _accessToken = getResponseToken(URLLoader(event.target));
176 dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
179 public function get accessToken():OAuthToken {
183 private function accessTokenError(event:IOErrorEvent):void {
184 if ( lastHTTPStatus == 401 ) {
185 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
187 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
189 deniedLabel.visible = true;
192 private function getConsumer():OAuthConsumer {
193 var key:String = Connection.getParam("oauth_consumer_key", "");
194 var secret:String = Connection.getParam("oauth_consumer_secret", "");
195 return new OAuthConsumer(key, secret);