make it look purty
[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.events.Event;
71         import flash.net.*;
72         import mx.managers.PopUpManager;
73         import net.systemeD.halcyon.connection.*;
74         import org.iotashan.oauth.*;
75
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;
81         
82         public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
83         
84         private function getAuthText():String {
85             return "To save data you must authorise this application to edit "+
86                     Connection.serverName + " on your behalf.";
87         }
88         
89         private function openURL(url:String):void {
90             var urlRequest:URLRequest = new URLRequest(url);
91             navigateToURL(urlRequest, "_blank");
92         }
93         
94         private function getRequestToken():void {
95             connection = Connection.getConnectionInstance();
96             
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");
100             
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)
104             
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);
111             loader.load(urlReq);
112         }
113         
114         private function recordStatus(event:HTTPStatusEvent):void {
115             lastHTTPStatus = event.status;
116         }
117         
118         private function requestTokenError(event:IOErrorEvent):void {
119             trace("error occured... last status was: "+lastHTTPStatus);
120             
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;
125             } else {
126                 contentStack.selectedChild = tempFailPanel;
127             }
128             progress.visible = false;
129         }
130         
131         private function loadedRequestToken(event:Event):void {
132             trace("Yay! response: "+URLLoader(event.target).data);
133             requestToken = getResponseToken(URLLoader(event.target));
134             
135             var url:String = Connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
136             link.label = url;
137             authoriseURL = url + "?oauth_token="+requestToken.key;
138             progress.visible = false;
139             gotLinkBox.visible = true;
140         }
141
142         private function getResponseToken(loader:URLLoader):OAuthToken {
143             var vars:URLVariables = new URLVariables(loader.data);
144             
145             // build out request token
146             var token:OAuthToken = new OAuthToken(
147                 String(vars["oauth_token"]),
148                 String(vars["oauth_token_secret"]));
149             return token;
150         }
151         
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");
156
157             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
158             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
159
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);
165             loader.load(urlReq);
166             
167             progress.label = "Checking access";
168             progress.visible = true;  
169         }
170         
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);
177             
178             _accessToken = getResponseToken(URLLoader(event.target));
179             Connection.getConnectionInstance().setAuthToken(_accessToken);
180             dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
181         }
182         
183         public function get accessToken():OAuthToken {
184             return _accessToken;
185         }
186         
187         public function get shouldRemember():Boolean {
188             return rememberMe.selected;
189         }
190         
191         private function accessTokenError(event:IOErrorEvent):void {
192             if ( lastHTTPStatus == 401 ) {
193                 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
194             } else {
195                 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
196             }
197             deniedLabel.visible = true;
198         }
199         
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);
204         }
205         
206         ]]></mx:Script>
207 </mx:TitleWindow>
208