Refactoring in progress
[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.getConnection();
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                         //register the "pressTry" function so the oAuth callback page can try to advance the editor directly to the next step
119                         ExternalInterface.addCallback("pressTry", pressTry);            
120                         
121             // build the actual request
122             var urlReq:URLRequest = new URLRequest(String(urlStr));
123             var loader:URLLoader = new URLLoader();
124             loader.addEventListener(Event.COMPLETE, loadedRequestToken);
125             loader.addEventListener(IOErrorEvent.IO_ERROR, requestTokenError);
126             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
127             loader.load(urlReq);
128         }
129         
130         private function recordStatus(event:HTTPStatusEvent):void {
131             lastHTTPStatus = event.status;
132         }
133         
134         private function requestTokenError(event:IOErrorEvent):void {
135             trace("error occured... last status was: "+lastHTTPStatus);
136             
137             if ( lastHTTPStatus == 401 ) {
138                 // this means authorisation was refused -- refused at this stage
139                 // means our consumer token is broken
140                 contentStack.selectedChild = permFailPanel;
141             } else {
142                 contentStack.selectedChild = tempFailPanel;
143             }
144             progress.visible = false;
145         }
146         
147         private function loadedRequestToken(event:Event):void {
148             trace("Yay! response: "+URLLoader(event.target).data);
149             requestToken = getResponseToken(URLLoader(event.target));
150             
151             var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
152             link.label = url;
153                         authoriseURL = url + "?oauth_token="+requestToken.key;
154                         progress.visible = false;
155             gotLinkBox.visible = true;
156         }
157
158         private function getResponseToken(loader:URLLoader):OAuthToken {
159             var vars:URLVariables = new URLVariables(loader.data);
160             
161             // build out request token
162             var token:OAuthToken = new OAuthToken(
163                 String(vars["oauth_token"]),
164                 String(vars["oauth_token_secret"]));
165             return token;
166         }
167         
168         private function getAccessToken():void {
169             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
170             var consumer:OAuthConsumer = getConsumer();
171             var url:String = Connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
172
173             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
174             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
175
176             var urlReq:URLRequest = new URLRequest(String(urlStr));
177             var loader:URLLoader = new URLLoader();
178             loader.addEventListener(Event.COMPLETE, loadedAccessToken);
179             loader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenError);
180             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
181             loader.load(urlReq);
182             
183             progress.label = "Checking access";
184             progress.visible = true;  
185         }
186         
187         private function loadedAccessToken(event:Event):void {
188             trace("Yay! response: "+URLLoader(event.target).data);
189             progress.label = "Received Access";
190             progress.indeterminate = false;
191             progress.setProgress(100,100);
192             PopUpManager.removePopUp(this);
193             
194             _accessToken = getResponseToken(URLLoader(event.target));
195             Connection.getConnection().setAuthToken(_accessToken);
196             dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
197         }
198         
199         public function get accessToken():OAuthToken {
200             return _accessToken;
201         }
202         
203         public function get shouldRemember():Boolean {
204             return rememberMe.selected;
205         }
206         
207         private function accessTokenError(event:IOErrorEvent):void {
208             if ( lastHTTPStatus == 401 ) {
209                 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
210             } else {
211                 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
212             }
213             deniedLabel.visible = true;
214         }
215         
216         private function getConsumer():OAuthConsumer {
217             var key:String = Connection.getParam("oauth_consumer_key", "");
218             var secret:String = Connection.getParam("oauth_consumer_secret", "");
219             return new OAuthConsumer(key, secret);
220         }
221                 
222                 public function pressTry():void {
223                         getAccessToken();
224                 }
225         
226         ]]></mx:Script>
227 </mx:TitleWindow>
228