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>
29 <mx:HBox width="100%" horizontalAlign="right">
30 <mx:CheckBox id="rememberMe" label="Remember authorisation" selected="true"/>
35 <mx:VBox id="permFailPanel" width="100%" height="100%">
36 <mx:Text styleName="failText" width="100%" condenseWhite="true">
37 <mx:htmlText><![CDATA[
38 <p>The server refused this application's credentials -- an authorisation link
39 could not be obtained.
41 <b>OAuth access will not be possible.</b>
43 Please contact application vendor to find out what's going on.
48 <mx:VBox id="tempFailPanel" width="100%" height="100%">
49 <mx:Text width="100%">
51 There was a problem contacting the server to get authorisation.
52 This may be a temporary error, try again later.
59 <mx:ControlBar horizontalAlign="right">
61 <mx:ProgressBar id="progress" label="Contacting server..." labelPlacement="top"
62 indeterminate="true"/>
63 <mx:Spacer width="100%"/>
65 <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
66 <mx:Button id="tryAccessButton" label="Try Access" click="getAccessToken()" enabled="false" styleName="titleWindowButton" />
70 import flash.events.Event;
72 import mx.managers.PopUpManager;
73 import net.systemeD.halcyon.connection.*;
74 import org.iotashan.oauth.*;
76 private var connection:Connection;
77 private var requestToken:OAuthToken;
78 private var _accessToken:OAuthToken;
79 private var authoriseURL:String;
80 private var lastHTTPStatus:int = 0;
82 public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
84 private function getAuthText():String {
85 return "To save data you must authorise this application to edit "+
86 Connection.serverName + " on your behalf.";
89 private function openURL(url:String):void {
90 var urlRequest:URLRequest = new URLRequest(url);
91 navigateToURL(urlRequest, "_blank");
94 private function getRequestToken():void {
95 connection = Connection.getConnectionInstance();
97 var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
98 var consumer:OAuthConsumer = getConsumer();
99 var url:String = Connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
101 var params:Object = new Object();
102 var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, params, consumer, null);
103 var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
105 // build the actual request
106 var urlReq:URLRequest = new URLRequest(String(urlStr));
107 var loader:URLLoader = new URLLoader();
108 loader.addEventListener(Event.COMPLETE, loadedRequestToken);
109 loader.addEventListener(IOErrorEvent.IO_ERROR, requestTokenError);
110 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
114 private function recordStatus(event:HTTPStatusEvent):void {
115 lastHTTPStatus = event.status;
118 private function requestTokenError(event:IOErrorEvent):void {
119 trace("error occured... last status was: "+lastHTTPStatus);
121 if ( lastHTTPStatus == 401 ) {
122 // this means authorisation was refused -- refused at this stage
123 // means our consumer token is broken
124 contentStack.selectedChild = permFailPanel;
126 contentStack.selectedChild = tempFailPanel;
128 progress.visible = false;
131 private function loadedRequestToken(event:Event):void {
132 trace("Yay! response: "+URLLoader(event.target).data);
133 requestToken = getResponseToken(URLLoader(event.target));
135 var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");
137 authoriseURL = url + "?oauth_token="+requestToken.key;
138 progress.visible = false;
139 gotLinkBox.visible = true;
142 private function getResponseToken(loader:URLLoader):OAuthToken {
143 var vars:URLVariables = new URLVariables(loader.data);
145 // build out request token
146 var token:OAuthToken = new OAuthToken(
147 String(vars["oauth_token"]),
148 String(vars["oauth_token_secret"]));
152 private function getAccessToken():void {
153 var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
154 var consumer:OAuthConsumer = getConsumer();
155 var url:String = Connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
157 var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
158 var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
160 var urlReq:URLRequest = new URLRequest(String(urlStr));
161 var loader:URLLoader = new URLLoader();
162 loader.addEventListener(Event.COMPLETE, loadedAccessToken);
163 loader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenError);
164 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
167 progress.label = "Checking access";
168 progress.visible = true;
171 private function loadedAccessToken(event:Event):void {
172 trace("Yay! response: "+URLLoader(event.target).data);
173 progress.label = "Received Access";
174 progress.indeterminate = false;
175 progress.setProgress(100,100);
176 PopUpManager.removePopUp(this);
178 _accessToken = getResponseToken(URLLoader(event.target));
179 Connection.getConnectionInstance().setAuthToken(_accessToken);
180 dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
183 public function get accessToken():OAuthToken {
187 public function get shouldRemember():Boolean {
188 return rememberMe.selected;
191 private function accessTokenError(event:IOErrorEvent):void {
192 if ( lastHTTPStatus == 401 ) {
193 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
195 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
197 deniedLabel.visible = true;
200 private function getConsumer():OAuthConsumer {
201 var key:String = Connection.getParam("oauth_consumer_key", "");
202 var secret:String = Connection.getParam("oauth_consumer_secret", "");
203 return new OAuthConsumer(key, secret);