Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for basic auth #3250

Merged
merged 12 commits into from
Oct 29, 2015
Merged

Add support for basic auth #3250

merged 12 commits into from
Oct 29, 2015

Conversation

zcbenz
Copy link
Member

@zcbenz zcbenz commented Oct 28, 2015

Close #1362
Close #625

Event: 'login'

Returns:

  • event Event
  • webContents WebContents
  • request Object
    • method String
    • url URL
    • referrer URL
  • authInfo Object
    • isProxy Boolean
    • scheme String
    • host String
    • port Integer
    • realm String
  • callback Function

Emitted when webContents wants to do basic auth.

The default behavior is to cancel all authentications, to override this you
should prevent the default behavior with event.preventDefault() and call
callback(username, password) with the credentials.

app.on('login', function(event, webContents, request, authInfo, callback) {
  event.preventDefault();
  callback('username', 'secret');
})

@anaisbetts
Copy link
Contributor

Nice, will this fix proxy authentication too?

@johnhaley81
Copy link
Contributor

This is awesome! ❤️

@zcbenz
Copy link
Member Author

zcbenz commented Oct 29, 2015

Nice, will this fix proxy authentication too?

Yeah.

zcbenz added a commit that referenced this pull request Oct 29, 2015
Add support for basic auth
@zcbenz zcbenz merged commit f13a02e into master Oct 29, 2015
@zcbenz zcbenz deleted the basic-auth branch October 29, 2015 02:20
@anaisbetts
Copy link
Contributor

@zcbenz This is great work, thank you so much!

@tommoor
Copy link
Contributor

tommoor commented Oct 29, 2015

😍

@clarkey
Copy link

clarkey commented Oct 29, 2015

Thanks @zcbenz. I'm hoping this will work with Windows Authentication via IIS. If anyone could build this on Windows it would be much appreciated! I'm in a corporate environment and it's just a pain to get all the dependencies installed!

@Torquin
Copy link

Torquin commented Oct 29, 2015

Thanks for this update ! Being new on Electron I'm wondering a few things if anyone could help ?

  • does electron need updating locally to include this new "login" event ?

  • how should i implement it ? Getting "407 Proxy Authentication Required" error after using app.commandLine.appendSwitch('proxy-server','http://...')

  • the "login" event doesn't seem to be triggering using the following :

    mainWindow.webContents.on('login', fcuntion(event, webContents, request, authInfo, callback) {
    event.preventDefault();
    mainWindow.webContents.executeJavaScript("alert('Proxy OK!')");
    callback('username', 'password');
    });

Thanks for any help guys ;)

@ocombe
Copy link

ocombe commented Oct 29, 2015

Would this open a connection modal asking for credentials (like we have in browsers) or is it something that we need to implement somehow ?

@anaisbetts
Copy link
Contributor

@clarkey I fixed this in 0.34.1, but you need to call an extra method to enable it: https://github.com/atom/electron/blob/master/docs/api/app.md#appallowntlmcredentialsforalldomainsallow

@clarkey
Copy link

clarkey commented Oct 30, 2015

@paulcbetts Thanks buddy. Got this working on the 0.34.2 release without using app.allowNTLMCredentialsForAllDomains - laavely!

@rodrigok
Copy link
Contributor

rodrigok commented Apr 5, 2016

@zcbenz Is there any way to add this event in webviews?

@sheepray
Copy link

sheepray commented Jun 2, 2016

Hi all, I am new to electron. I am wondering when the event 'login' is triggered. And what does the 'login' even mean in here? Couple lines of example codes are appreciated. Thanks.

@patrykcieszkowski
Copy link

patrykcieszkowski commented Oct 18, 2016

this doesn't fix proxy issue. Event is getting fired, but after authorisation nothing happens if you run proxy from window other than main browser.

@cphoover
Copy link

cphoover commented Nov 3, 2016

How do we trigger a modal to prompt for input?

@wiliame
Copy link

wiliame commented Nov 28, 2016

Will be nice to clear auth credentials. For example, I'm using a proxy that accepts options via username:

proxy.com:8080
user: wiliam_country_US

So, if I want to change the peer node, I change my name:

proxy.com:8080
user: wiliam_country_GB

But, I'm not changing the proxy, so electron has valid credentials and is not going to trigger login event.

I'm searching the way to clean auth settings so electron asks them again.

(I have created a feature request: #8089)

@georgiosd
Copy link

georgiosd commented Dec 1, 2016

Hello,

This doesn't seem to work on the webContents.on('login') event - even though it gets called, I get a 407. Any way to fix?

Thanks

EDIT: it works if you call event.preventDefault() on the app.on('login') callback

@vadim-ch
Copy link

Hello,
How to make it work with autoUpdate???

@wiliame
Copy link

wiliame commented Dec 13, 2016

Clear auth data is fixed in electron 1.4.12:
#8089

@shrynx
Copy link

shrynx commented Feb 3, 2017

webContent returned in the function has nodeIntegration : false, How do i set it to true or can we do it by default ?

@ericuldall
Copy link

Is there any suggested workflow for incorrect credentials getting sent to the callback? Currently I have a workflow where if a user trys to click a link to one of my Basic Auth pages it pops a modal over the new window with a login prompt. Assuming I enter correct credentials it works as expected however there doesn't seem to be any way of knowing if the user passed invalid credentials at which point I end up with a blank window.

Has anyone else run into this problem and/or is there something obvious i'm missing?

@vsanse
Copy link

vsanse commented Aug 25, 2019

Getting Error while implementing basic authentication:
Code:

app.on('login', (event, webContents, request, authInfo, callback) => {
  event.preventDefault()
  console.log(callback)
  secWindow = new BrowserWindow({
    width:200,
    height:200,
    parent:mainWindow,
    modal:true,
    webPreferences: {
      nodeIntegration: true
      }
  });
  secWindow.loadFile('./renderer/secondarywindows/login.html');
  ipcMain.on("loginchannel",(e,args)=>{
    loginCred = args;
    if(loginCred.canceled){
      callback()
    }
    else{
      callback(loginCred.username, loginCred.password);
    }
    secWindow = null;
  })
})
Uncaught Exception:
Error: callback can only be called for once
    at EventEmitter.<anonymous>

Error occurs when I try to cancel first time and submit next time, so in short login event is fired twice.Please help what am I doing wrong.

@MatthieuLeMee
Copy link

callback()

You should do nothing when user cancels login. Like said by Electron, you cannot call callback() twice.
Calling it without args is useless anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTTP Basic Authentication support Proxy support Basic Authentication?