Browse Source

auth: new function has_credentials(realm)

- returns true if an authorization header matching the realm is found
Daniel-Constantin Mierla 13 years ago
parent
commit
1a0d765313
3 changed files with 87 additions and 20 deletions
  1. 33 19
      modules/auth/README
  2. 35 0
      modules/auth/auth_mod.c
  3. 19 1
      modules/auth/doc/functions.xml

+ 33 - 19
modules/auth/README

@@ -42,12 +42,13 @@ Daniel-Constantin Mierla
    1.4. Functions
    1.4. Functions
 
 
         1.4.1. consume_credentials()
         1.4.1. consume_credentials()
-        1.4.2. www_challenge(realm, flags)
-        1.4.3. proxy_challenge(realm, flags)
-        1.4.4. auth_challenge(realm, flags)
-        1.4.5. pv_www_authenticate(realm, passwd, flags)
-        1.4.6. pv_proxy_authenticate(realm, passwd, flags)
-        1.4.7. auth_get_www_authenticate(realm, flags, pvdst)
+        1.4.2. has_credentials(realm)
+        1.4.3. www_challenge(realm, flags)
+        1.4.4. proxy_challenge(realm, flags)
+        1.4.5. auth_challenge(realm, flags)
+        1.4.6. pv_www_authenticate(realm, passwd, flags)
+        1.4.7. pv_proxy_authenticate(realm, passwd, flags)
+        1.4.8. auth_get_www_authenticate(realm, flags, pvdst)
 
 
 1.1. Overview
 1.1. Overview
 
 
@@ -526,12 +527,25 @@ modparam("auth", "realm_prefix", "sip.")
 
 
    Example 15. consume_credentials example
    Example 15. consume_credentials example
 ...
 ...
-if (www_authenticate("realm", "subscriber)) {
+if (www_authenticate("realm", "subscriber")) {
     consume_credentials();
     consume_credentials();
 };
 };
 ...
 ...
 
 
-1.4.2. www_challenge(realm, flags)
+1.4.2. has_credentials(realm)
+
+   This function returns true of the request has Autorization or
+   Proxy-Authorization header with provided realm. The parameter can be
+   string with pseudo-variables.
+
+   Example 16. consume_credentials example
+...
+if (has_credentials("myrealm")) {
+    ...
+}
+...
+
+1.4.3. www_challenge(realm, flags)
 
 
    The function challenges a user agent. It will generate a WWW-Authorize
    The function challenges a user agent. It will generate a WWW-Authorize
    header field containing a digest challenge, it will put the header
    header field containing a digest challenge, it will put the header
@@ -560,14 +574,14 @@ if (www_authenticate("realm", "subscriber)) {
 
 
    This function can be used from REQUEST_ROUTE.
    This function can be used from REQUEST_ROUTE.
 
 
-   Example 16. www_challenge usage
+   Example 17. www_challenge usage
 ...
 ...
 if (!www_authenticate("$td", "subscriber")) {
 if (!www_authenticate("$td", "subscriber")) {
         www_challenge("$td", "1");
         www_challenge("$td", "1");
 }
 }
 ...
 ...
 
 
-1.4.3. proxy_challenge(realm, flags)
+1.4.4. proxy_challenge(realm, flags)
 
 
    The function challenges a user agent. It will generate a
    The function challenges a user agent. It will generate a
    Proxy-Authorize header field containing a digest challenge, it will put
    Proxy-Authorize header field containing a digest challenge, it will put
@@ -582,14 +596,14 @@ if (!www_authenticate("$td", "subscriber")) {
 
 
    This function can be used from REQUEST_ROUTE.
    This function can be used from REQUEST_ROUTE.
 
 
-   Example 17. proxy_challenge usage
+   Example 18. proxy_challenge usage
 ...
 ...
 if (!proxy_authenticate("$fd", "subscriber")) {
 if (!proxy_authenticate("$fd", "subscriber")) {
         proxy_challenge("$fd", "1");
         proxy_challenge("$fd", "1");
 };
 };
 ...
 ...
 
 
-1.4.4. auth_challenge(realm, flags)
+1.4.5. auth_challenge(realm, flags)
 
 
    The function challenges a user agent for authentication. It combines
    The function challenges a user agent for authentication. It combines
    the functions www_challenge() and proxy_challenge(), by calling
    the functions www_challenge() and proxy_challenge(), by calling
@@ -601,14 +615,14 @@ if (!proxy_authenticate("$fd", "subscriber")) {
 
 
    This function can be used from REQUEST_ROUTE.
    This function can be used from REQUEST_ROUTE.
 
 
-   Example 18. proxy_challenge usage
+   Example 19. proxy_challenge usage
 ...
 ...
 if (!auth_check("$fd", "subscriber", "1")) {
 if (!auth_check("$fd", "subscriber", "1")) {
         auth_challenge("$fd", "1");
         auth_challenge("$fd", "1");
 };
 };
 ...
 ...
 
 
-1.4.5. pv_www_authenticate(realm, passwd, flags)
+1.4.6. pv_www_authenticate(realm, passwd, flags)
 
 
    The function verifies credentials according to RFC2617. If the
    The function verifies credentials according to RFC2617. If the
    credentials are verified successfully then the function will succeed
    credentials are verified successfully then the function will succeed
@@ -652,14 +666,14 @@ if (!auth_check("$fd", "subscriber", "1")) {
 
 
    This function can be used from REQUEST_ROUTE.
    This function can be used from REQUEST_ROUTE.
 
 
-   Example 19. pv_www_authenticate usage
+   Example 20. pv_www_authenticate usage
 ...
 ...
 if (!pv_www_authenticate("$td", "123abc", "0")) {
 if (!pv_www_authenticate("$td", "123abc", "0")) {
         www_challenge("$td", "1");
         www_challenge("$td", "1");
 };
 };
 ...
 ...
 
 
-1.4.6. pv_proxy_authenticate(realm, passwd, flags)
+1.4.7. pv_proxy_authenticate(realm, passwd, flags)
 
 
    The function verifies credentials according to RFC2617. If the
    The function verifies credentials according to RFC2617. If the
    credentials are verified successfully then the function will succeed
    credentials are verified successfully then the function will succeed
@@ -674,7 +688,7 @@ if (!pv_www_authenticate("$td", "123abc", "0")) {
 
 
    This function can be used from REQUEST_ROUTE.
    This function can be used from REQUEST_ROUTE.
 
 
-   Example 20. pv_proxy_authenticate usage
+   Example 21. pv_proxy_authenticate usage
 ...
 ...
 $avp(password)="xyz";
 $avp(password)="xyz";
 if (!pv_proxy_authenticate("$fd", "$avp(password)", "0")) {
 if (!pv_proxy_authenticate("$fd", "$avp(password)", "0")) {
@@ -682,7 +696,7 @@ if (!pv_proxy_authenticate("$fd", "$avp(password)", "0")) {
 };
 };
 ...
 ...
 
 
-1.4.7. auth_get_www_authenticate(realm, flags, pvdst)
+1.4.8. auth_get_www_authenticate(realm, flags, pvdst)
 
 
    Build WWW-Authentication header and set the resulting value in 'pvdest'
    Build WWW-Authentication header and set the resulting value in 'pvdest'
    parameter.
    parameter.
@@ -692,7 +706,7 @@ if (!pv_proxy_authenticate("$fd", "$avp(password)", "0")) {
 
 
    This function can be used from ANY_ROUTE.
    This function can be used from ANY_ROUTE.
 
 
-   Example 21. auth_get_www_authenticate
+   Example 22. auth_get_www_authenticate
 ...
 ...
 if (auth_get_www_authenticate("$fd", "0", "$var(wauth)")) {
 if (auth_get_www_authenticate("$fd", "0", "$var(wauth)")) {
         xlog("www authenticate header is [$var(wauth)]\n");
         xlog("www authenticate header is [$var(wauth)]\n");

+ 35 - 0
modules/auth/auth_mod.c

@@ -81,6 +81,10 @@ static int mod_init(void);
  * Remove used credentials from a SIP message header
  * Remove used credentials from a SIP message header
  */
  */
 int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2);
 int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2);
+/*
+ * Check for credentials with given realm
+ */
+int w_has_credentials(struct sip_msg* msg, char* s1, char* s2);
 
 
 static int pv_proxy_authenticate(struct sip_msg* msg, char* realm,
 static int pv_proxy_authenticate(struct sip_msg* msg, char* realm,
 		char *passwd, char *flags);
 		char *passwd, char *flags);
@@ -160,6 +164,8 @@ static cmd_export_t cmds[] = {
 			fixup_pv_auth, REQUEST_ROUTE},
 			fixup_pv_auth, REQUEST_ROUTE},
     {"auth_get_www_authenticate",  (cmd_function)w_auth_get_www_authenticate,  3,
     {"auth_get_www_authenticate",  (cmd_function)w_auth_get_www_authenticate,  3,
 			fixup_auth_get_www_authenticate, REQUEST_ROUTE},
 			fixup_auth_get_www_authenticate, REQUEST_ROUTE},
+    {"has_credentials",        w_has_credentials,                    1,
+			fixup_spve_null, REQUEST_ROUTE},
     {"bind_auth_s",           (cmd_function)bind_auth_s, 0, 0, 0        },
     {"bind_auth_s",           (cmd_function)bind_auth_s, 0, 0, 0        },
     {0, 0, 0, 0, 0}
     {0, 0, 0, 0, 0}
 };
 };
@@ -408,6 +414,35 @@ int w_consume_credentials(struct sip_msg* msg, char* s1, char* s2)
 	return consume_credentials(msg);
 	return consume_credentials(msg);
 }
 }
 
 
+/**
+ *
+ */
+int w_has_credentials(sip_msg_t *msg, char* realm, char* s2)
+{
+    str srealm  = {0, 0};
+	hdr_field_t *hdr = NULL;
+	int ret;
+
+	if (fixup_get_svalue(msg, (gparam_t*)realm, &srealm) < 0) {
+		LM_ERR("failed to get realm value\n");
+		return -1;
+	}
+
+	ret = find_credentials(msg, &srealm, HDR_PROXYAUTH_T, &hdr);
+	if(ret==0) {
+		LM_DBG("found www credentials with realm [%.*s]\n", srealm.len, srealm.s);
+		return 1;
+	}
+	ret = find_credentials(msg, &srealm, HDR_AUTHORIZATION_T, &hdr);
+	if(ret==0) {
+		LM_DBG("found proxy credentials with realm [%.*s]\n", srealm.len, srealm.s);
+		return 1;
+	}
+
+	LM_DBG("no credentials with realm [%.*s]\n", srealm.len, srealm.s);
+	return -1;
+}
+
 /**
 /**
  * @brief do WWW-Digest authentication with password taken from cfg var
  * @brief do WWW-Digest authentication with password taken from cfg var
  */
  */

+ 19 - 1
modules/auth/doc/functions.xml

@@ -24,9 +24,27 @@
 	    <title>consume_credentials example</title>
 	    <title>consume_credentials example</title>
 	    <programlisting>
 	    <programlisting>
 ...
 ...
-if (www_authenticate("realm", "subscriber)) {
+if (www_authenticate("realm", "subscriber")) {
     consume_credentials();
     consume_credentials();
 };
 };
+...
+	    </programlisting>
+	</example>
+    </section>
+    <section id="has_credentials">
+	<title><function>has_credentials(realm)</function></title>
+	<para>
+		This function returns true of the request has Autorization or
+		Proxy-Authorization header with provided realm. The parameter
+		can be string with pseudo-variables.
+	</para>
+	<example>
+	    <title>consume_credentials example</title>
+	    <programlisting>
+...
+if (has_credentials("myrealm")) {
+    ...
+}
 ...
 ...
 	    </programlisting>
 	    </programlisting>
 	</example>
 	</example>