Blame src/HttpAuth.h

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 2016 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#ifndef HTTPAUTH_H
Packit 8f70b4
#define HTTPAUTH_H
Packit 8f70b4
Packit 8f70b4
#include "xmap.h"
Packit 8f70b4
#include "xarray.h"
Packit 8f70b4
#include "HttpHeader.h"
Packit 8f70b4
Packit 8f70b4
class HttpAuth
Packit 8f70b4
{
Packit 8f70b4
public:
Packit 8f70b4
   enum target_t { WWW=0, PROXY };
Packit 8f70b4
   enum scheme_t { NONE=0, BASIC, DIGEST };
Packit 8f70b4
Packit 8f70b4
   class Challenge
Packit 8f70b4
   {
Packit 8f70b4
      scheme_t scheme_code;
Packit 8f70b4
      xstring scheme;
Packit 8f70b4
      xmap_p<xstring> param;
Packit 8f70b4
Packit 8f70b4
      void SetParam(const char *p,int p_len,const xstring &v) {
Packit 8f70b4
	 param.add(xstring::get_tmp(p,p_len).c_lc(),new xstring(v.copy()));
Packit 8f70b4
      }
Packit 8f70b4
      void SetParam(const xstring &p,const xstring &v) {
Packit 8f70b4
	 param.add(p,new xstring(v.copy()));
Packit 8f70b4
      }
Packit 8f70b4
   public:
Packit 8f70b4
      Challenge(const char *);
Packit 8f70b4
      const xstring& GetScheme() { return scheme; }
Packit 8f70b4
      scheme_t GetSchemeCode() { return scheme_code; }
Packit 8f70b4
      const xstring& GetRealm() { return GetParam("realm"); }
Packit 8f70b4
      const xstring& GetParam(const char *p) {
Packit 8f70b4
	 const xstring *v=param.lookup(p);
Packit 8f70b4
	 return v?*v:xstring::null;
Packit 8f70b4
      }
Packit 8f70b4
   };
Packit 8f70b4
Packit 8f70b4
protected:
Packit 8f70b4
   target_t target;
Packit 8f70b4
   xstring uri;
Packit 8f70b4
   Ref<Challenge> chal;
Packit 8f70b4
   xstring user;
Packit 8f70b4
   xstring pass;
Packit 8f70b4
   HttpHeader header;
Packit 8f70b4
Packit 8f70b4
   static xstring& append_quoted(xstring& s,const char *n,const char *v);
Packit 8f70b4
Packit 8f70b4
   // array is enough as there are not too many HttpAuth objects.
Packit 8f70b4
   static xarray_p<HttpAuth> cache;
Packit 8f70b4
Packit 8f70b4
public:
Packit 8f70b4
   HttpAuth(target_t t,const char *p_uri,Challenge *p_chal,const char *p_user,const char *p_pass)
Packit 8f70b4
      :  target(t), uri(p_uri), chal(p_chal), user(p_user), pass(p_pass),
Packit 8f70b4
	 header(t==WWW?"Authorization":"Proxy-Authorization") {}
Packit 8f70b4
   virtual ~HttpAuth() {}
Packit 8f70b4
   virtual bool IsValid() const { return true; }
Packit 8f70b4
   virtual bool Update(const char *p_method,const char *p_uri,const char *entity_hash=0) { return true; }
Packit 8f70b4
   const HttpHeader *GetHeader() { return &header; }
Packit 8f70b4
   bool ApplicableForURI(const char *) const;
Packit 8f70b4
   bool Matches(target_t t,const char *p_uri,const char *p_user);
Packit 8f70b4
Packit 8f70b4
   static bool New(target_t t,const char *p_uri,
Packit 8f70b4
      Challenge *p_chal,const char *p_user,const char *p_pass);
Packit 8f70b4
   static HttpAuth *Get(target_t t,const char *p_uri,const char *p_user);
Packit 8f70b4
   static void CleanCache(target_t t,const char *p_uri,const char *p_user);
Packit 8f70b4
};
Packit 8f70b4
Packit 8f70b4
class HttpAuthBasic : public HttpAuth
Packit 8f70b4
{
Packit 8f70b4
   void MakeHeader();
Packit 8f70b4
public:
Packit 8f70b4
   HttpAuthBasic(target_t t,const char *p_uri,Challenge *p_chal,const char *p_user,const char *p_pass)
Packit 8f70b4
      :  HttpAuth(t,p_uri,p_chal,p_user,p_pass) { MakeHeader(); }
Packit 8f70b4
};
Packit 8f70b4
Packit 8f70b4
class HttpAuthDigest : public HttpAuth
Packit 8f70b4
{
Packit 8f70b4
   xstring cnonce;   // random client nonce
Packit 8f70b4
   xstring HA1;	     // "session key" A1 in lower-case hex
Packit 8f70b4
   unsigned nc;
Packit 8f70b4
   void MakeHA1();
Packit 8f70b4
public:
Packit 8f70b4
   HttpAuthDigest(target_t t,const char *p_uri,Challenge *p_chal,const char *p_user,const char *p_pass)
Packit 8f70b4
      :  HttpAuth(t,p_uri,p_chal,p_user,p_pass), nc(0) { MakeHA1(); }
Packit 8f70b4
   bool IsValid() const { return HA1.length()>0; }
Packit 8f70b4
   bool Update(const char *p_method,const char *p_uri,const char *entity_hash);
Packit 8f70b4
};
Packit 8f70b4
Packit 8f70b4
#endif//HTTPAUTH_H