How do I authenticate, using Nim's httpclient module to retrieve HTML? - authentication

I'm a beginner and I want to write a Nim-application that processes some data from an internal website.
Basic authentication (username, password) is required to access this site.
A working Python solution is:
response = requests.get('https://internal:PORT/page',
auth=('user', 'passwd'),
verify=False) # this is vital
Based on the nim doc regarding httpclient and the modules source code, where it is stated that one could use a proxy as an argument for any of the functions, I've been trying something along these lines:
var
client = newHttpClient()
prox = newProxy("https://internal:PORT/page", "user:passwd")
let response = client.getContent(prox) # Error: type mismatch
The solution is probably very obvious but I'm out of ideas on
how to authenticate.
If anybody could help, that'd be highly appreciated!

Basic auth is just an "Authorization" header with value "Basic " + base64(username + ":" + password). Equivalent in nim:
import httpclient, base64
var
client = newHttpClient()
var username = ...
var password = ...
client.headers["Authorization"] = "Basic " & base64.encode(username & ":" & password)
# ... send request with the client

Related

403 access denied to the website with proper login/pass through google script

var url = "https://web-site_name/page/?format=json&var_data-organization_dates&xlsexport=true";
var payload =
{
"login" : "login",
"password" : "pass",
};
var options =
{
"method" : "post",
"payload" : payload,
"followRedirects" : false
};
var login = UrlFetchApp.fetch("https://web-site_name/page/" , options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(login.getAllHeaders());
here is the part of the code I try to use, to automate export of the data from web-site, i do have proper login and password and able to download file in json (opened in xsl) manually, I've got the address to the downloaded file in network in developer tools, but i have a problem on the first stage - when trying to authorize to the web-site - access denied. I've tried the code, given in answers on stackoverflow, but it still doesn't work.
How to make an url fetch request correctly, depends on the website you want to access and the authentication they uses
In the simplest case, your website requires HTTP basic authentification, in this case the correct syntax would be
var authHeader = 'Basic ' + Utilities.base64Encode(login + ':' + pass);
var options = {
headers: {Authorization: authHeader}
}
If your website uses a different authentication form, you might need to provide an access token.
In any case: the authentication credentials go into headers, not into payload!
payload is the data that you want to post = upload to the website.
If you want export data from the website - that is download data - you do not need a payload and the correct method would be get, not post. Btw., if the method is get, you do not need to specify it.
Please see here for more information and samples.

Monzo API: Invalid request: required parameter client_id is unknown

I keep getting the following error Invalid request: required parameter client_id is unknown when making a request to the monzo auth api to get an access token. I am getting the client_id from the developer playground response using GET /ping/whoami.
I am then putting this into my request:
let clientID = "oauthclient_XXXXXXXXXXXXXXXX"
let baseURL = "https://auth.monzo.com/"
let redirectURI = "https://Monzo-AR.novoda.com"
let responseType = "code"
let stateToken = "random string"
var requestURL: String!
requestURL = baseURL +
"?client_id=" +
clientID +
"&redirect_uri=" +
redirectURI +
"&response_type=" +
responseType +
"&state=" +
stateToken
Can anyone see what i am doing wrong?
The /ping/whoami endpoint returns the client_id for the Developer Console (which was used to authenticate you for that service)
It's not suggested to use that client_id in your own applications. If you head to the Monzo Clients Page you will be able to create your own client and receive an ID for it.
Additionally, the redirect URI must match that of the one configured in the clients page linked before (You will get an error otherwise)
You haven't given context to what you're doing with the requestURL - You will need to redirect the user to this page in order to authenticate.
Once you have been redirected to the authentication page at the link you've constructed, you'll be able to use your browsers console (Cmd + Option + J on Chrome Mac) to see any errors that present themselves

How to use UrlFetchApp with credentials? Google Scripts

I am trying to use Google Scripts UrlFetchApp to access a website with a basic username and password. As soon as I connect to the site a popup appears that requires authentication. I know the Login and Password, however I do not know how to pass them within the UrlFetchApp.
var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/‎");
Logger.log(response.getContentText("UTF-8"));
Currently running that code returns "Access Denied". The above code does not contain the actual address I am connecting to for security reasons. A "t" is missing from all the "http" in the code examples because they are being detected as links and Stackoverflow does not allow me to submit more than two links.
How can I pass the Login and Password along with my request? Also is there anyway I can continue my session once I have logged in? Or will my next UrlFetchApp request be sent from another Google server requiring me to login again?
The goal here is to login to the website behind Googles network infrastructure so it can act as a proxy then I need to issue another UrlFetchApp request to the same address that would look something like this:
var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/vuze/rpc?json={"method":"torrent-add","arguments":{"filename":"htp://vodo.net/media/torrents/anything.torrent","download-dir":"C:\\temp"}}‎");
Logger.log(response.getContentText("UTF-8"));
This question has been answered on another else where.
Here is the summary:
Bruce Mcpherson
basic authentication looks like this...
var options = {};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};
Lenny Cunningham
//Added Basic Authorization//////////////////////////////////////////////////////////////////////////////////////////
var USERNAME = PropertiesService.getScriptProperties().getProperty('username');
var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');
var url = PropertiesService.getScriptProperties().getProperty('url');//////////////////////////Forwarded
Ports to WebRelay
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
};
var params = {
"method":"GET",
"headers":headers
};
var reponse = UrlFetchApp.fetch(url, params);
I was unable to find user3586062's source links (they may have been deleted), but taking Bruce Mcpherson's approach, your code would look like this:
var options = {};
options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};
UrlFetchApp.fetch("TARGET URL GOES HERE", options);

cordova/phonegap 3.3: how to set user credentials in fileUploadOptions

I'm trying to make a file upload via Phonegap 3.3 file transfer plugin to a windows server secured by base authentication. Actually the normal conversation between my app and the server (per ajax) is working perfectly by sending my user credentials with every ajax call.
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.headers = {
'Authorization':authHeaderValue(db.getItem("user"), db.getItem("pass"))
};
and
authHeaderValue = function(username, password) {
var tok = username + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
};
This is what I tried so far (I found it on stackoverflow thread) but it gives me back a 401-unauthorized...
Pls. give me a short reply if you know something that could help me.
Best regards to you all,
Ingmar
Well, I do something similar but instead of "Basic" I use JWT for authentication. I'll show you the code I use:
options.headers = { 'Authorization': 'Bearer ' + app.session.getSess('token') };
And I use SessionStorage to save the token while it is valid.
If you wanna know about JSON Web Token
Another thing, remember to change the headers in your server, in my case something like:
('Access-Control-Allow-Origin','*');
('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS');
('Access-Control-Allow-Headers','Content-Type, Authorization, Content-Length, X-Requested-With');

How to get github token using username and password

I am developing mobile apps using rhodes. I want to access private repo of github. I am having only username and password.
How to get token of given username and password.
Once you have only login and password you can use them using basic auth. First of all, check if this code shows you json data of desired repo. Username and password must be separated by a colon.
curl -u "user:pwd" https://api.github.com/repos/user/repo
If succeeded you should consider doing this request from code.
import urllib2
import json
from StringIO import StringIO
import base64
username = "user#example.com"
password = "naked_password"
req = urllib2.Request("https://api.github.com/repos/user/repo")
req.add_header("Authorization", "Basic " + base64.urlsafe_b64encode("%s:%s" % (username, password)))
req.add_header("Content-Type", "application/json")
req.add_header("Accept", "application/json")
res = urllib2.urlopen(req)
data = res.read()
repository = json.load(StringIO(data))
You should use oauth instead: http://developer.github.com/v3/oauth/
Github users can create Personal Access Tokens at their application settings. You can use this token as an alternative to username/password in basic http authentication to call the API or to access private repositories on the github website.
Simply use a client that supports basic http authentication. Set the username equal to the token, and the password equal to x-oauth-basic. For example with curl:
curl -u <token>:x-oauth-basic https://api.github.com/user
See also https://developer.github.com/v3/auth/.
Send A POST request to /authorizations
With headers
Content-Type: application/json
Accept: application/json
Authorization: Basic base64encode(<username>:<password>)
But remember to take Two factor Authentication in mind
https://developer.github.com/v3/auth/#working-with-two-factor-authentication
Here You will receive a token which can be used for further request
Follow this guide at help.github.com. It describes how to find your api-token (it's under "Account Settings" > "Account Admin") and configuring git so it uses the token.
Here is the code to use GitHub Basic Authentication in JavaScript
let username = "*******";
let password = "******";
let auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
host: 'api.github.com',
path: '/search/repositories?q=google%20maps%20api',
method: 'GET',
headers: {
'user-agent': 'node.js',
"Authorization": auth
}
};
var request = https.request(options, function (res) {
}));