github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/defaultimpl/DefaultAuthenticator_test.go (about)

     1  package defaultimpl
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  	"testing"
     7  
     8  	"github.com/Ingenico-ePayments/connect-sdk-go/communicator/communication"
     9  )
    10  
    11  func TestToCanonicalizedHeaderValue(t *testing.T) {
    12  	result, err := toCanonicalizedHeaderValue("aap\nnoot  ")
    13  	if err != nil {
    14  		t.Fatalf("TestToCanonicalizedHeaderValue : %v", err)
    15  	}
    16  	if result != "aap noot" {
    17  		t.Fatalf("TestToCanonicalizedHeaderValue : wrong canonization 1 '%s' != '%s'",
    18  			result, "aap noot")
    19  	}
    20  
    21  	result, err = toCanonicalizedHeaderValue("aap\r\n  noot")
    22  	if err != nil {
    23  		t.Fatalf("TestToCanonicalizedHeaderValue : %v", err)
    24  	}
    25  	if result != "aap noot" {
    26  		t.Fatalf("TestToCanonicalizedHeaderValue : wrong canonization 2 '%s' != '%s'",
    27  			result, "aap noot")
    28  	}
    29  
    30  	result, err = toCanonicalizedHeaderValue(" some value  \r\n \n with  some \r\n  spaces ")
    31  	if err != nil {
    32  		t.Fatalf("TestToCanonicalizedHeaderValue : %v", err)
    33  	}
    34  	if result != "some value    with  some  spaces" {
    35  		t.Fatalf("TestToCanonicalizedHeaderValue : wrong canonization 3 '%s' != '%s'",
    36  			result, "some value    with  some  spaces")
    37  	}
    38  }
    39  
    40  func substr(s string, pos, length int) string {
    41  	runes := []rune(s)
    42  	l := pos + length
    43  	if l > len(runes) {
    44  		l = len(runes)
    45  	}
    46  	return string(runes[pos:l])
    47  }
    48  
    49  func TestToDataToSign(t *testing.T) {
    50  	httpHeaders := []communication.Header{}
    51  	serverMetaInfoHeader, err := communication.NewHeader("X-GCS-ServerMetaInfo", "{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}")
    52  	if err != nil {
    53  		t.Fatalf("TestToDataToSign : %v", err)
    54  	}
    55  	httpHeaders = append(httpHeaders, *serverMetaInfoHeader)
    56  
    57  	contentTypeHeader, err := communication.NewHeader("Content-Type", "application/json")
    58  	if err != nil {
    59  		t.Fatalf("TestToDataToSign : %v", err)
    60  	}
    61  	httpHeaders = append(httpHeaders, *contentTypeHeader)
    62  
    63  	clientMetaInfoHeader, err := communication.NewHeader("X-GCS-ClientMetaInfo", "{\"aap\",\"noot\"}")
    64  	if err != nil {
    65  		t.Fatalf("TestToDataToSign : %v", err)
    66  	}
    67  	httpHeaders = append(httpHeaders, *clientMetaInfoHeader)
    68  
    69  	userAgentHeader, err := communication.NewHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)")
    70  	if err != nil {
    71  		t.Fatalf("TestToDataToSign : %v", err)
    72  	}
    73  	httpHeaders = append(httpHeaders, *userAgentHeader)
    74  
    75  	dateHeader, err := communication.NewHeader("Date", "Mon, 07 Jul 2014 12:12:40 GMT")
    76  	if err != nil {
    77  		t.Fatalf("TestToDataToSign : %v", err)
    78  	}
    79  	httpHeaders = append(httpHeaders, *dateHeader)
    80  
    81  	urlArg, err := url.Parse("http://localhost:8080/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot")
    82  	if err != nil {
    83  		t.Fatalf("TestToDataToSign : %v", err)
    84  	}
    85  
    86  	dataToSign, err := toDataToSign("POST", *urlArg, httpHeaders)
    87  	if err != nil {
    88  		t.Fatalf("TestToDataToSign : %v", err)
    89  	}
    90  
    91  	expectedStart := "POST\napplication/json\n"
    92  	expectedEnd := "x-gcs-clientmetainfo:{\"aap\",\"noot\"}\nx-gcs-servermetainfo:{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}\n/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen noot\n"
    93  
    94  	actualStart := substr(dataToSign, 0, 22)
    95  	actualEnd := substr(dataToSign, 52, len(dataToSign)-52)
    96  
    97  	if expectedStart != actualStart {
    98  		t.Fatalf("TestToDataToSign : expectedStart '%s' != '%s'", expectedStart, actualStart)
    99  	}
   100  	if expectedEnd != actualEnd {
   101  		t.Fatalf("TestToDataToSign : expectedEnd '%s' != '%s'", expectedEnd, actualEnd)
   102  	}
   103  }
   104  
   105  func TestCreateAuthenticationSignature(t *testing.T) {
   106  	auth, err := NewDefaultAuthenticator(V1HMAC, "apiKeyId", "secretApiKey")
   107  	if err != nil {
   108  		t.Fatalf("TestCreateAuthenticationSignature : %v", err)
   109  	}
   110  
   111  	dataToSign := "DELETE\napplication/json\nFri, 06 Jun 2014 13:39:43 GMT\nx-gcs-clientmetainfo:processed header value\nx-gcs-customerheader:processed header value\nx-gcs-servermetainfo:processed header value\n/v1/9991/tokens/123456789\n"
   112  
   113  	authenticationSignature, err := auth.signData(dataToSign)
   114  	if err != nil {
   115  		t.Fatalf("TestCreateAuthenticationSignature : %v", err)
   116  	}
   117  	if authenticationSignature != "VfnXpPBQQoHZivTcAg0JvOWkhnzlPnaCPKpTQn/uMJM=" {
   118  		t.Fatalf("TestCreateAuthenticationSignature : authenticationSignature '%s' != '%s'",
   119  			"VfnXpPBQQoHZivTcAg0JvOWkhnzlPnaCPKpTQn/uMJM=", authenticationSignature)
   120  	}
   121  }
   122  
   123  func TestCreateAuthenticationSignature2(t *testing.T) {
   124  	auth, err := NewDefaultAuthenticator(V1HMAC, "EC36A74A98D21", "6Kj5HT0MQKC6D8eb7W3lTg71kVKVDSt1")
   125  	if err != nil {
   126  		t.Fatalf("TestCreateAuthenticationSignature2 : %v", err)
   127  	}
   128  
   129  	dataToSign := "GET\n\nFri, 06 Jun 2014 13:39:43 GMT\n/v1/9991/tokens/123456789\n"
   130  
   131  	authenticationSignature, err := auth.signData(dataToSign)
   132  	if err != nil {
   133  		t.Fatalf("TestCreateAuthenticationSignature2 : %v", err)
   134  	}
   135  	if authenticationSignature != "9ond5EIN05dBXJGCLRK5om9pxHsyrh/12pZJ7bvmwNM=" {
   136  		t.Fatalf("TestCreateAuthenticationSignature2 : authenticationSignature '%s' != '%s'",
   137  			"9ond5EIN05dBXJGCLRK5om9pxHsyrh/12pZJ7bvmwNM=", authenticationSignature)
   138  	}
   139  }
   140  
   141  func TestTotalMinimalExample(t *testing.T) {
   142  	authenticator, err := NewDefaultAuthenticator(V1HMAC, "5e45c937b9db33ae", "I42Zf4pVnRdroHfuHnRiJjJ2B6+22h0yQt/R3nZR8Xg=")
   143  	if err != nil {
   144  		t.Fatalf("TestTotalMinimalExample : %v", err)
   145  	}
   146  
   147  	httpHeaders := []communication.Header{}
   148  
   149  	userAgentHeader, err := communication.NewHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)")
   150  	if err != nil {
   151  		t.Fatalf("TestTotalMinimalExample : %v", err)
   152  	}
   153  	httpHeaders = append(httpHeaders, *userAgentHeader)
   154  
   155  	dateHeader, err := communication.NewHeader("Date", "Fri, 06 Jun 2014 13:39:43 GMT")
   156  	if err != nil {
   157  		t.Fatalf("TestTotalMinimalExample : %v", err)
   158  	}
   159  	httpHeaders = append(httpHeaders, *dateHeader)
   160  	parsedURL, err := url.Parse("http://world.api-ingenico.com:8080/v1/9991/tokens/123456789")
   161  	if err != nil {
   162  		t.Fatalf("TestTotalMinimalExample : %v", err)
   163  	}
   164  
   165  	signature, err := authenticator.CreateSimpleAuthenticationSignature(http.MethodGet, *parsedURL, httpHeaders)
   166  	if err != nil {
   167  		t.Fatalf("TestTotalMinimalExample : %v", err)
   168  	}
   169  	if signature != "GCS v1HMAC:5e45c937b9db33ae:J5LjfSBvrQNhu7gG0gvifZt+IWNDReGCmHmBmth6ueI=" {
   170  		t.Fatalf("TestTotalMinimalExample : signature '%s' != '%s'",
   171  			"GCS v1HMAC:5e45c937b9db33ae:J5LjfSBvrQNhu7gG0gvifZt+IWNDReGCmHmBmth6ueI=", signature)
   172  	}
   173  }
   174  
   175  func TestTotalFullExample(t *testing.T) {
   176  	authenticator, err := NewDefaultAuthenticator(V1HMAC, "5e45c937b9db33ae", "I42Zf4pVnRdroHfuHnRiJjJ2B6+22h0yQt/R3nZR8Xg=")
   177  	if err != nil {
   178  		t.Fatalf("TestTotalFullExample : %v", err)
   179  	}
   180  
   181  	httpHeaders := []communication.Header{}
   182  
   183  	userAgentHeader, err := communication.NewHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)")
   184  	if err != nil {
   185  		t.Fatalf("TestTotalFullExample : %v", err)
   186  	}
   187  	httpHeaders = append(httpHeaders, *userAgentHeader)
   188  
   189  	serverMetaInfoHeader, err := communication.NewHeader("X-GCS-ServerMetaInfo", "processed header value")
   190  	if err != nil {
   191  		t.Fatalf("TestTotalFullExample : %v", err)
   192  	}
   193  	httpHeaders = append(httpHeaders, *serverMetaInfoHeader)
   194  
   195  	clientMetaInfoHeader, err := communication.NewHeader("X-GCS-ClientMetaInfo", "processed header value")
   196  	if err != nil {
   197  		t.Fatalf("TestTotalFullExample : %v", err)
   198  	}
   199  	httpHeaders = append(httpHeaders, *clientMetaInfoHeader)
   200  
   201  	contentTypeHeader, err := communication.NewHeader("Content-Type", "application/json")
   202  	if err != nil {
   203  		t.Fatalf("TestTotalFullExample : %v", err)
   204  	}
   205  	httpHeaders = append(httpHeaders, *contentTypeHeader)
   206  
   207  	customerHeader, err := communication.NewHeader("X-GCS-CustomerHeader", "processed header value")
   208  	if err != nil {
   209  		t.Fatalf("TestTotalFullExample : %v", err)
   210  	}
   211  	httpHeaders = append(httpHeaders, *customerHeader)
   212  
   213  	dateHeader, err := communication.NewHeader("Date", "Fri, 06 Jun 2014 13:39:43 GMT")
   214  	if err != nil {
   215  		t.Fatalf("TestTotalFullExample : %v", err)
   216  	}
   217  	httpHeaders = append(httpHeaders, *dateHeader)
   218  
   219  	parsedURL, err := url.Parse("http://world.api-ingenico.com:8080/v1/9991/tokens/123456789")
   220  	if err != nil {
   221  		t.Fatalf("TestTotalFullExample : %v", err)
   222  	}
   223  
   224  	signature, err := authenticator.CreateSimpleAuthenticationSignature(http.MethodDelete, *parsedURL, httpHeaders)
   225  	if err != nil {
   226  		t.Fatalf("TestTotalFullExample : %v", err)
   227  	}
   228  	if signature != "GCS v1HMAC:5e45c937b9db33ae:jGWLz3ouN4klE+SkqO5gO+KkbQNM06Rric7E3dcfmqw=" {
   229  		t.Fatalf("TestTotalFullExample : signature '%s' != '%s'",
   230  			"GCS v1HMAC:5e45c937b9db33ae:jGWLz3ouN4klE+SkqO5gO+KkbQNM06Rric7E3dcfmqw=", signature)
   231  	}
   232  }