2014-02-17 11:19:18 -05:00
const Format = imports . format ;
const Gettext = imports . gettext ;
2019-02-08 22:21:36 -05:00
const { Gio , GLib , GObject , Gtk , Pango , Soup , WebKit2 : WebKit } = imports . gi ;
2014-02-17 11:19:18 -05:00
const _ = Gettext . gettext ;
const Config = imports . misc . config ;
2018-09-05 20:55:20 -04:00
const { loadInterfaceXML } = imports . misc . fileUtils ;
2014-02-17 11:19:18 -05:00
const PortalHelperResult = {
CANCELLED : 0 ,
COMPLETED : 1 ,
RECHECK : 2
} ;
2017-02-10 15:08:01 -05:00
const PortalHelperSecurityLevel = {
NOT _YET _DETERMINED : 0 ,
SECURE : 1 ,
INSECURE : 2
} ;
2014-02-17 11:19:18 -05:00
const INACTIVITY _TIMEOUT = 30000 ; //ms
2016-10-24 10:58:24 -04:00
const CONNECTIVITY _CHECK _HOST = 'nmcheck.gnome.org' ;
const CONNECTIVITY _CHECK _URI = 'http://' + CONNECTIVITY _CHECK _HOST ;
2014-02-17 11:19:18 -05:00
const CONNECTIVITY _RECHECK _RATELIMIT _TIMEOUT = 30 * GLib . USEC _PER _SEC ;
2018-09-05 20:55:20 -04:00
const HelperDBusInterface = loadInterfaceXML ( 'org.gnome.Shell.PortalHelper' ) ;
2014-02-17 11:19:18 -05:00
2017-10-30 21:23:39 -04:00
var PortalHeaderBar = GObject . registerClass (
class PortalHeaderBar extends Gtk . HeaderBar {
2017-10-30 20:03:21 -04:00
_init ( ) {
2017-10-30 21:23:39 -04:00
super . _init ( { show _close _button : true } ) ;
2017-02-10 15:08:01 -05:00
// See ephy-title-box.c in epiphany for the layout
let vbox = new Gtk . Box ( { orientation : Gtk . Orientation . VERTICAL ,
spacing : 0 } ) ;
this . set _custom _title ( vbox ) ;
/* TRANSLATORS: this is the title of the wifi captive portal login window */
let titleLabel = new Gtk . Label ( { label : _ ( "Hotspot Login" ) ,
wrap : false ,
single _line _mode : true ,
ellipsize : Pango . EllipsizeMode . END } ) ;
titleLabel . get _style _context ( ) . add _class ( 'title' ) ;
vbox . add ( titleLabel ) ;
let hbox = new Gtk . Box ( { orientation : Gtk . Orientation . HORIZONTAL ,
spacing : 4 ,
halign : Gtk . Align . CENTER ,
valign : Gtk . Align . BASELINE } ) ;
hbox . get _style _context ( ) . add _class ( 'subtitle' ) ;
vbox . add ( hbox ) ;
this . _lockImage = new Gtk . Image ( { icon _size : Gtk . IconSize . MENU ,
valign : Gtk . Align . BASELINE } ) ;
hbox . add ( this . _lockImage ) ;
this . subtitleLabel = new Gtk . Label ( { wrap : false ,
single _line _mode : true ,
ellipsize : Pango . EllipsizeMode . END ,
valign : Gtk . Align . BASELINE ,
selectable : true } ) ;
this . subtitleLabel . get _style _context ( ) . add _class ( 'subtitle' ) ;
hbox . add ( this . subtitleLabel ) ;
vbox . show _all ( ) ;
2017-10-30 21:23:39 -04:00
}
2017-02-10 15:08:01 -05:00
2017-10-30 20:03:21 -04:00
setSubtitle ( label ) {
2017-02-10 15:08:01 -05:00
this . subtitleLabel . set _text ( label ) ;
2017-10-30 21:23:39 -04:00
}
2017-02-10 15:08:01 -05:00
2017-10-30 20:03:21 -04:00
setSecurityIcon ( securityLevel ) {
2017-02-10 15:08:01 -05:00
switch ( securityLevel ) {
case PortalHelperSecurityLevel . NOT _YET _DETERMINED :
this . _lockImage . hide ( ) ;
break ;
case PortalHelperSecurityLevel . SECURE :
this . _lockImage . show ( ) ;
this . _lockImage . set _from _icon _name ( "channel-secure-symbolic" , Gtk . IconSize . MENU ) ;
this . _lockImage . set _tooltip _text ( null ) ;
break ;
case PortalHelperSecurityLevel . INSECURE :
this . _lockImage . show ( ) ;
this . _lockImage . set _from _icon _name ( "channel-insecure-symbolic" , Gtk . IconSize . MENU ) ;
this . _lockImage . set _tooltip _text ( _ ( 'Your connection to this hotspot login is not secure. Passwords or other information you enter on this page can be viewed by people nearby.' ) ) ;
break ;
}
2017-10-30 21:23:39 -04:00
}
2017-02-10 15:08:01 -05:00
} ) ;
2017-10-30 21:23:39 -04:00
var PortalWindow = GObject . registerClass (
class PortalWindow extends Gtk . ApplicationWindow {
2017-10-30 20:03:21 -04:00
_init ( application , url , timestamp , doneCallback ) {
2017-10-30 21:23:39 -04:00
super . _init ( { application : application } ) ;
2017-01-23 02:32:30 -05:00
2017-12-01 19:27:35 -05:00
this . connect ( 'delete-event' , this . destroyWindow . bind ( this ) ) ;
2017-02-10 15:08:01 -05:00
this . _headerBar = new PortalHeaderBar ( ) ;
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . NOT _YET _DETERMINED ) ;
2017-01-23 02:32:30 -05:00
this . set _titlebar ( this . _headerBar ) ;
this . _headerBar . show ( ) ;
2014-02-17 11:19:18 -05:00
2014-07-28 03:36:40 -04:00
if ( ! url ) {
2016-10-24 10:58:24 -04:00
url = CONNECTIVITY _CHECK _URI ;
2014-07-28 03:36:40 -04:00
this . _originalUrlWasGnome = true ;
} else {
this . _originalUrlWasGnome = false ;
2014-02-17 11:19:18 -05:00
}
2014-07-28 03:36:40 -04:00
this . _uri = new Soup . URI ( url ) ;
this . _everSeenRedirect = false ;
2014-02-17 11:19:18 -05:00
this . _originalUrl = url ;
this . _doneCallback = doneCallback ;
this . _lastRecheck = 0 ;
this . _recheckAtExit = false ;
2017-03-23 11:54:28 -04:00
this . _webContext = WebKit . WebContext . new _ephemeral ( ) ;
2017-02-13 04:44:01 -05:00
this . _webContext . set _cache _model ( WebKit . CacheModel . DOCUMENT _VIEWER ) ;
2017-05-18 12:13:12 -04:00
this . _webContext . set _network _proxy _settings ( WebKit . NetworkProxyMode . NO _PROXY , null ) ;
2017-01-23 01:14:17 -05:00
2017-02-13 04:44:01 -05:00
this . _webView = WebKit . WebView . new _with _context ( this . _webContext ) ;
2017-12-01 19:27:35 -05:00
this . _webView . connect ( 'decide-policy' , this . _onDecidePolicy . bind ( this ) ) ;
this . _webView . connect ( 'load-changed' , this . _onLoadChanged . bind ( this ) ) ;
this . _webView . connect ( 'insecure-content-detected' , this . _onInsecureContentDetected . bind ( this ) ) ;
this . _webView . connect ( 'load-failed-with-tls-errors' , this . _onLoadFailedWithTlsErrors . bind ( this ) ) ;
2014-02-17 11:19:18 -05:00
this . _webView . load _uri ( url ) ;
2017-12-01 19:27:35 -05:00
this . _webView . connect ( 'notify::uri' , this . _syncUri . bind ( this ) ) ;
2017-01-23 02:32:30 -05:00
this . _syncUri ( ) ;
2014-02-17 11:19:18 -05:00
this . add ( this . _webView ) ;
this . _webView . show ( ) ;
2017-01-23 02:32:05 -05:00
this . set _size _request ( 600 , 450 ) ;
2014-02-17 11:19:18 -05:00
this . maximize ( ) ;
this . present _with _time ( timestamp ) ;
2017-01-17 09:01:01 -05:00
this . application . set _accels _for _action ( 'app.quit' , [ '<Primary>q' , '<Primary>w' ] ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
destroyWindow ( ) {
2017-01-23 01:14:17 -05:00
this . destroy ( ) ;
2017-10-30 21:23:39 -04:00
}
2017-01-23 01:14:17 -05:00
2017-10-30 20:03:21 -04:00
_syncUri ( ) {
2017-01-23 02:32:30 -05:00
let uri = this . _webView . uri ;
if ( uri )
2017-02-10 15:08:01 -05:00
this . _headerBar . setSubtitle ( GLib . uri _unescape _string ( uri , null ) ) ;
2017-01-23 02:32:30 -05:00
else
2017-02-13 05:10:19 -05:00
this . _headerBar . setSubtitle ( '' ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
refresh ( ) {
2014-02-17 11:19:18 -05:00
this . _everSeenRedirect = false ;
this . _webView . load _uri ( this . _originalUrl ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
vfunc _delete _event ( event ) {
2014-02-17 11:19:18 -05:00
if ( this . _recheckAtExit )
this . _doneCallback ( PortalHelperResult . RECHECK ) ;
else
this . _doneCallback ( PortalHelperResult . CANCELLED ) ;
return false ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
_onLoadChanged ( view , loadEvent ) {
2017-03-23 13:27:40 -04:00
if ( loadEvent == WebKit . LoadEvent . STARTED ) {
2017-02-10 15:08:01 -05:00
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . NOT _YET _DETERMINED ) ;
2017-03-23 13:27:40 -04:00
} else if ( loadEvent == WebKit . LoadEvent . COMMITTED ) {
2017-02-10 15:08:01 -05:00
let tlsInfo = this . _webView . get _tls _info ( ) ;
let ret = tlsInfo [ 0 ] ;
let flags = tlsInfo [ 2 ] ;
if ( ret && flags == 0 )
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . SECURE ) ;
else
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . INSECURE ) ;
}
2017-10-30 21:23:39 -04:00
}
2017-02-10 15:08:01 -05:00
2017-10-30 20:03:21 -04:00
_onInsecureContentDetected ( ) {
2017-02-10 15:08:01 -05:00
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . INSECURE ) ;
2017-10-30 21:23:39 -04:00
}
2017-02-10 15:08:01 -05:00
2017-10-30 20:03:21 -04:00
_onLoadFailedWithTlsErrors ( view , failingURI , certificate , errors ) {
2017-02-13 04:44:01 -05:00
this . _headerBar . setSecurityIcon ( PortalHelperSecurityLevel . INSECURE ) ;
let uri = new Soup . URI ( failingURI ) ;
this . _webContext . allow _tls _certificate _for _host ( certificate , uri . get _host ( ) ) ;
this . _webView . load _uri ( failingURI ) ;
return true ;
2017-10-30 21:23:39 -04:00
}
2017-02-13 04:44:01 -05:00
2017-10-30 20:03:21 -04:00
_onDecidePolicy ( view , decision , type ) {
2014-02-17 11:19:18 -05:00
if ( type == WebKit . PolicyDecisionType . NEW _WINDOW _ACTION ) {
2017-03-24 13:57:29 -04:00
let navigationAction = decision . get _navigation _action ( ) ;
if ( navigationAction . is _user _gesture ( ) ) {
// Even though the portal asks for a new window,
// perform the navigation in the current one. Some
// portals open a window as their last login step and
// ignoring that window causes them to not let the
// user go through. We don't risk popups taking over
// the page because we check that the navigation is
// user initiated.
this . _webView . load _request ( navigationAction . get _request ( ) ) ;
}
2014-02-17 11:19:18 -05:00
decision . ignore ( ) ;
return true ;
}
if ( type != WebKit . PolicyDecisionType . NAVIGATION _ACTION )
return false ;
let request = decision . get _request ( ) ;
let uri = new Soup . URI ( request . get _uri ( ) ) ;
2014-07-28 03:36:40 -04:00
if ( ! uri . host _equal ( this . _uri ) && this . _originalUrlWasGnome ) {
2016-10-24 10:58:24 -04:00
if ( uri . get _host ( ) == CONNECTIVITY _CHECK _HOST && this . _everSeenRedirect ) {
2014-02-17 11:19:18 -05:00
// Yay, we got to gnome!
decision . ignore ( ) ;
this . _doneCallback ( PortalHelperResult . COMPLETED ) ;
return true ;
2016-10-24 10:58:24 -04:00
} else if ( uri . get _host ( ) != CONNECTIVITY _CHECK _HOST ) {
2014-02-17 11:19:18 -05:00
this . _everSeenRedirect = true ;
}
}
2014-07-28 03:36:40 -04:00
// We *may* have finished here, but we don't know for
// sure. Tell gnome-shell to run another connectivity check
// (but ratelimit the checks, we don't want to spam
// nmcheck.gnome.org for portals that have 10 or more internal
// redirects - and unfortunately they exist)
// If we hit the rate limit, we also queue a recheck
// when the window is closed, just in case we miss the
// final check and don't realize we're connected
// This should not be a problem in the cancelled logic,
// because if the user doesn't want to start the login,
// we should not see any redirect at all, outside this._uri
let now = GLib . get _monotonic _time ( ) ;
let shouldRecheck = ( now - this . _lastRecheck ) >
CONNECTIVITY _RECHECK _RATELIMIT _TIMEOUT ;
if ( shouldRecheck ) {
this . _lastRecheck = now ;
this . _recheckAtExit = false ;
this . _doneCallback ( PortalHelperResult . RECHECK ) ;
} else {
this . _recheckAtExit = true ;
}
// Update the URI, in case of chained redirects, so we still
// think we're doing the login until gnome-shell kills us
this . _uri = uri ;
2014-02-17 11:19:18 -05:00
decision . use ( ) ;
return true ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
} ) ;
2017-10-30 21:23:39 -04:00
var WebPortalHelper = GObject . registerClass (
class WebPortalHelper extends Gtk . Application {
2017-10-30 20:03:21 -04:00
_init ( ) {
2017-10-30 21:23:39 -04:00
super . _init ( { application _id : 'org.gnome.Shell.PortalHelper' ,
2014-02-17 11:19:18 -05:00
flags : Gio . ApplicationFlags . IS _SERVICE ,
inactivity _timeout : 30000 } ) ;
this . _dbusImpl = Gio . DBusExportedObject . wrapJSObject ( HelperDBusInterface , this ) ;
this . _queue = [ ] ;
2017-01-17 09:01:01 -05:00
let action = new Gio . SimpleAction ( { name : 'quit' } ) ;
2017-01-23 01:14:17 -05:00
action . connect ( 'activate' , ( ) => { this . active _window . destroyWindow ( ) ; } ) ;
2017-01-17 09:01:01 -05:00
this . add _action ( action ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
vfunc _dbus _register ( connection , path ) {
2014-02-17 11:19:18 -05:00
this . _dbusImpl . export ( connection , path ) ;
2017-10-30 21:23:39 -04:00
super . vfunc _dbus _register ( connection , path ) ;
2014-02-17 11:19:18 -05:00
return true ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
vfunc _dbus _unregister ( connection , path ) {
2014-02-17 11:19:18 -05:00
this . _dbusImpl . unexport _from _connection ( connection ) ;
2017-10-30 21:23:39 -04:00
super . vfunc _dbus _unregister ( connection , path ) ;
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
vfunc _activate ( ) {
2014-02-17 11:19:18 -05:00
// If launched manually (for example for testing), force a dummy authentication
// session with the default url
this . Authenticate ( '/org/gnome/dummy' , '' , 0 ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
Authenticate ( connection , url , timestamp ) {
2014-02-17 11:19:18 -05:00
this . _queue . push ( { connection : connection , url : url , timestamp : timestamp } ) ;
this . _processQueue ( ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
Close ( connection ) {
2014-02-17 11:19:18 -05:00
for ( let i = 0 ; i < this . _queue . length ; i ++ ) {
let obj = this . _queue [ i ] ;
if ( obj . connection == connection ) {
if ( obj . window )
2017-01-23 01:14:17 -05:00
obj . window . destroyWindow ( ) ;
2014-02-17 11:19:18 -05:00
this . _queue . splice ( i , 1 ) ;
break ;
}
}
this . _processQueue ( ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
Refresh ( connection ) {
2014-02-17 11:19:18 -05:00
for ( let i = 0 ; i < this . _queue . length ; i ++ ) {
let obj = this . _queue [ i ] ;
if ( obj . connection == connection ) {
if ( obj . window )
obj . window . refresh ( ) ;
break ;
}
}
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
2017-10-30 20:03:21 -04:00
_processQueue ( ) {
2014-02-17 11:19:18 -05:00
if ( this . _queue . length == 0 )
return ;
let top = this . _queue [ 0 ] ;
if ( top . window != null )
return ;
2017-10-30 20:38:18 -04:00
top . window = new PortalWindow ( this , top . url , top . timestamp , result => {
2014-02-17 11:19:18 -05:00
this . _dbusImpl . emit _signal ( 'Done' , new GLib . Variant ( '(ou)' , [ top . connection , result ] ) ) ;
2017-10-30 20:38:18 -04:00
} ) ;
2017-10-30 21:23:39 -04:00
}
2014-02-17 11:19:18 -05:00
} ) ;
function initEnvironment ( ) {
String . prototype . format = Format . format ;
}
function main ( argv ) {
initEnvironment ( ) ;
2017-03-23 11:54:28 -04:00
if ( ! WebKit . WebContext . new _ephemeral ) {
log ( 'WebKitGTK 2.16 is required for the portal-helper, see https://bugzilla.gnome.org/show_bug.cgi?id=780453' ) ;
return 1 ;
}
2014-02-17 11:19:18 -05:00
Gettext . bindtextdomain ( Config . GETTEXT _PACKAGE , Config . LOCALEDIR ) ;
Gettext . textdomain ( Config . GETTEXT _PACKAGE ) ;
let app = new WebPortalHelper ( ) ;
return app . run ( argv ) ;
}