authenticate


Description:

public virtual signal void authenticate (Message msg, Auth auth, bool retrying)

Emitted when the session requires authentication.

If credentials are available call authenticate on auth. If these credentials fail, the signal will be emitted again, with retrying set to true, which will continue until you return without calling authenticate on auth.

Note that this may be emitted before msg's body has been fully read.

If you call pause_message on msg before returning, then you can authenticate auth asynchronously (as long as you @ref it to make sure it doesn't get destroyed), and then unpause msg when you are ready for it to continue.

Example: Authentication, sync:

public static int main (string[] args) {
// Create a session:
Soup.Session session = new Soup.Session ();

// Register authentication handler:
int counter = 0;
session.authenticate.connect ((msg, auth, retrying) => {
if (counter < 3) {
if (retrying == true) {
print ("Invalid user name or password.\n");
}

print ("Username: ");
string username = stdin.read_line ();

print ("Password: ");
string passwd = stdin.read_line ();

auth.authenticate (username, passwd);
counter++;
}
});


print ("URL: ");
string url = stdin.read_line ();

// Send a request:
Soup.Message msg = new Soup.Message ("GET", url);
if (msg == null) {
print ("Invalid URL\n");
return 0;
}

session.send_message (msg);

// Process the result:
print ("Status Code: %u\n", msg.status_code);
return 0;
}

valac --pkg libsoup-2.4 authentication-sync.vala

Example: Authentication, async:

public static int main (string[] args) {
MainLoop loop = new MainLoop ();


print ("URL: ");
string url = stdin.read_line ();

print ("Username: ");
string username = stdin.read_line ();

print ("Password: ");
string passwd = stdin.read_line ();


// Create a session:
Soup.Session session = new Soup.Session ();

// Register authentication handler:
session.authenticate.connect ((msg, auth, retrying) => {
if (retrying == false) {
print ("Start authetnication:\n");

// Simulate asynchronous input / time consuming operations:
// See GLib.IOSchedulerJob for time consuming operations
Timeout.add_seconds (10, () => {
print ("Authentication\n");
auth.authenticate (username, passwd);

// Resumes HTTP I/O on msg:
session.unpause_message (msg);
return false;
}, Priority.DEFAULT);

// Pauses HTTP I/O on msg:
session.pause_message (msg);
}
});

// Send a request:
Soup.Message msg = new Soup.Message ("GET", url);
if (msg == null) {
print ("Invalid URL\n");
return 0;
}

session.queue_message (msg, (sess, mess) => {
// Process the result:
print ("Status Code: %u\n", mess.status_code);
loop.quit ();
});

loop.run ();
return 0;
}

valac --pkg libsoup-2.4 authentication-async.vala

Parameters:

msg

the Message being sent

auth

the Auth to authenticate

retrying

true if this is the second (or later) attempt




2022 vala-language.org