making oAuth open up in a smaller centred window
[potlatch2.git] / net / systemeD / potlatch2 / save / OAuthPanel.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical"
5         horizontalAlign="center" title="Authorisation Required"
6         creationComplete="getRequestToken()"
7         height="290">
8         
9         <mx:ViewStack id="contentStack" width="100%" height="100%">
10         
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%">
15               <mx:text>
16                 Click the link below to open a web page where
17                 you will be asked to authorise access to this app.
18               </mx:text>
19             </mx:Text>
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> <!-- ' -->
25             </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>
28             </mx:Text>
29             <mx:HBox width="100%" horizontalAlign="right">
30               <mx:CheckBox id="rememberMe" label="Remember authorisation" selected="true"/>
31             </mx:HBox>
32           </mx:VBox>
33         </mx:VBox>
34         
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.
40               </p><p>
41               <b>OAuth access will not be possible.</b>
42               </p><p>
43               Please contact application vendor to find out what's going on.
44             ]]></mx:htmlText>
45           </mx:Text>
46         </mx:VBox>
47         
48         <mx:VBox id="tempFailPanel" width="100%" height="100%">
49           <mx:Text width="100%">
50             <mx:text>
51               There was a problem contacting the server to get authorisation.
52               This may be a temporary error, try again later.
53             </mx:text>
54           </mx:Text>
55         </mx:VBox>
56         
57         </mx:ViewStack>
58         
59         <mx:ControlBar horizontalAlign="right">
60         
61             <mx:ProgressBar id="progress" label="Contacting server..." labelPlacement="top"
62                 indeterminate="true"/>
63         <mx:Spacer width="100%"/>
64
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" />
67         </mx:ControlBar>
68         
69         <mx:Script><![CDATA[
70                 import flash.display.InteractiveObject;
71         import flash.events.Event;
72         import flash.net.*;
73                 import flash.system.Capabilities;
74         import mx.managers.PopUpManager;
75         import net.systemeD.halcyon.connection.*;
76         import org.iotashan.oauth.*;
77                 import flash.external.ExternalInterface;
78
79         private var connection:Connection;
80         private var requestToken:OAuthToken;
81         private var _accessToken:OAuthToken;
82         private var authoriseURL:String;
83         private var lastHTTPStatus:int = 0;
84         
85         public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
86         
87         private function getAuthText():String {
88             return "To save data you must authorise this application to edit "+
89                     Connection.serverName + " on your behalf.";
90         }
91         
92         private function openURL(url:String):void {
93                         if (ExternalInterface.available) {
94                                 var winH:int = 560;
95                                 var winW:int = 600;
96                                 var leftPos:int  = (Capabilities.screenResolutionX - winW) / 2;
97                                 var topPos:int = (Capabilities.screenResolutionY - winH) / 2;
98                                 ExternalInterface.call( "window.open", url,"oAuthWin","height=" + winH + ",width=" + winW +",top=" + topPos + ", left=" + leftPos +", toolbar=no,scrollbars=no,status=no,location=no,menubar=no,directories=no");
99                                 }               
100                         else
101                         {
102                                 var urlRequest:URLRequest = new URLRequest(url);
103                                 navigateToURL(urlRequest,"_blank");
104                         }
105                 }
106         
107         private function getRequestToken():void {
108             connection = Connection.getConnectionInstance();
109             
110             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
111             var consumer:OAuthConsumer = getConsumer();
112             var url:String = Connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
113             
114             var params:Object = new Object();
115             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, params, consumer, null);
116             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
117             
118             // build the actual request
119             var urlReq:URLRequest = new URLRequest(String(urlStr));
120             var loader:URLLoader = new URLLoader();
121             loader.addEventListener(Event.COMPLETE, loadedRequestToken);
122             loader.addEventListener(IOErrorEvent.IO_ERROR, requestTokenError);
123             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
124             loader.load(urlReq);
125         }
126         
127         private function recordStatus(event:HTTPStatusEvent):void {
128             lastHTTPStatus = event.status;
129         }
130         
131         private function requestTokenError(event:IOErrorEvent):void {
132             trace("error occured... last status was: "+lastHTTPStatus);
133             
134             if ( lastHTTPStatus == 401 ) {
135                 // this means authorisation was refused -- refused at this stage
136                 // means our consumer token is broken
137                 contentStack.selectedChild = permFailPanel;
138             } else {
139                 contentStack.selectedChild = tempFailPanel;
140             }
141             progress.visible = false;
142         }
143         
144         private function loadedRequestToken(event:Event):void {
145             trace("Yay! response: "+URLLoader(event.target).data);
146             requestToken = getResponseToken(URLLoader(event.target));
147             
148             var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
149             link.label = url;
150                         authoriseURL = url + "?oauth_token="+requestToken.key;
151                         progress.visible = false;
152             gotLinkBox.visible = true;
153         }
154
155         private function getResponseToken(loader:URLLoader):OAuthToken {
156             var vars:URLVariables = new URLVariables(loader.data);
157             
158             // build out request token
159             var token:OAuthToken = new OAuthToken(
160                 String(vars["oauth_token"]),
161                 String(vars["oauth_token_secret"]));
162             return token;
163         }
164         
165         private function getAccessToken():void {
166             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
167             var consumer:OAuthConsumer = getConsumer();
168             var url:String = Connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
169
170             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
171             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
172
173             var urlReq:URLRequest = new URLRequest(String(urlStr));
174             var loader:URLLoader = new URLLoader();
175             loader.addEventListener(Event.COMPLETE, loadedAccessToken);
176             loader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenError);
177             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
178             loader.load(urlReq);
179             
180             progress.label = "Checking access";
181             progress.visible = true;  
182         }
183         
184         private function loadedAccessToken(event:Event):void {
185             trace("Yay! response: "+URLLoader(event.target).data);
186             progress.label = "Received Access";
187             progress.indeterminate = false;
188             progress.setProgress(100,100);
189             PopUpManager.removePopUp(this);
190             
191             _accessToken = getResponseToken(URLLoader(event.target));
192             Connection.getConnectionInstance().setAuthToken(_accessToken);
193             dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
194         }
195         
196         public function get accessToken():OAuthToken {
197             return _accessToken;
198         }
199         
200         public function get shouldRemember():Boolean {
201             return rememberMe.selected;
202         }
203         
204         private function accessTokenError(event:IOErrorEvent):void {
205             if ( lastHTTPStatus == 401 ) {
206                 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
207             } else {
208                 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
209             }
210             deniedLabel.visible = true;
211         }
212         
213         private function getConsumer():OAuthConsumer {
214             var key:String = Connection.getParam("oauth_consumer_key", "");
215             var secret:String = Connection.getParam("oauth_consumer_secret", "");
216             return new OAuthConsumer(key, secret);
217         }
218         
219         ]]></mx:Script>
220 </mx:TitleWindow>
221