This is the sample program that can see the decode result of barcode data on Watson IoT.

Dependencies:   AsciiFont DisplayApp GR-PEACH_video LCD_shield_config LWIPBP3595Interface_STA_for_mbed-os USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PassphraseAuthenticator.cpp Source File

PassphraseAuthenticator.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    PassphraseAuthenticator.cpp
00003  * @brief   mbed CoAP Endpoint Device Management Passphrase-based Authenticator class
00004  * @author  Doug Anson
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2016
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022  
00023 // Class support
00024 #include "mbed-connector-interface/PassphraseAuthenticator.h"
00025 
00026 // demarshalling support
00027 #include "mbed-client/m2mresource.h"
00028  
00029 // constructor
00030 PassphraseAuthenticator::PassphraseAuthenticator(const Logger *logger,const void *passphrase) : Authenticator(logger,passphrase) {
00031 }
00032 
00033 // copy constructor
00034 PassphraseAuthenticator::PassphraseAuthenticator(const PassphraseAuthenticator &authenticator) : Authenticator(authenticator) {
00035 }
00036 
00037 // destructor
00038 PassphraseAuthenticator::~PassphraseAuthenticator() {
00039 }
00040 
00041 // basic (trivial passphrase authentication)
00042 bool PassphraseAuthenticator::authenticate(void *challenge) {
00043     // use simple, trivial passphrase based comparison as the check... 
00044     char *passphrase = (char *)this->m_secret;
00045     char *input_passphrase = NULL;
00046     
00047 #if defined (HAS_EXECUTE_PARAMS)
00048     // ExecParam mbed-client: un-marshall the ExecuteParameter to get the simple string...
00049     M2MResource::M2MExecuteParameter* param = (M2MResource::M2MExecuteParameter*)challenge;
00050     if (param != NULL) {
00051         // use parameters to extract the passphrase
00052         String object_name = param->get_argument_object_name();
00053         // int instance_id = (int)param->get_argument_object_instance_id();
00054         String resource_name = param->get_argument_resource_name();
00055         string value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
00056         input_passphrase = (char *)value.c_str();
00057     }
00058 #else 
00059     // Non-ExecParam mbed-client: use the parameter directly... 
00060     input_passphrase = (char *)challenge;
00061 #endif
00062     
00063     // DEBUG
00064     //this->m_logger->logging("Authenticator(passphrase): passphrase: [%s] challenge: [%s]",passphrase,input_passphrase);
00065     
00066     // parameter checks...the compare passphrases and return the result
00067     if (passphrase != NULL && input_passphrase != NULL && strcmp(passphrase,input_passphrase) == 0) {
00068         // DEBUG
00069         this->m_logger->logging("Authenticator(passphrase): Passphrases MATCH. Authenticated.");
00070     
00071         // passphrases match
00072         return true;
00073     }
00074     
00075     // DEBUG
00076     this->m_logger->logging("Authenticator(passphrase): Passphrases do not match");
00077     
00078     // authentication failure
00079     return false;
00080 }
00081 
00082 // convenience method to get the URI from its buffer field...
00083 string PassphraseAuthenticator::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) {
00084     if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
00085         char buf[MAX_VALUE_BUFFER_LENGTH+1];
00086         memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
00087         memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
00088         return string(buf);
00089     }
00090     return string("");
00091 }