Authentication

Giới thiệu


Tất cả các request đến Phoebus REST API server đều cần Authentication Token.

Để sử dụng Phoebus API, trước tiên, người dùng cần đăng nhập vào hệ thống. Sau khi đăng nhập thành công, hệ thống sẽ trả về thông tin người dùng cùng Authentication Token.

Token được sử dụng cho các request tiếp theo đến Server. Server sẽ yêu cầu người dùng gửi kèm Token nếu Token không được gửi kèm trong request.

Sử dụng


Đăng nhập bằng tài khoản tích hợp

Khi tích hợp với các phần mềm khác, ta cần tạo 1 tài khoản Operator Definition và ban quyền truy cập cho tài khoản này.

Để đăng nhập với tài khoản tích hợp, người dùng sử dụng API sau:

GET ServiceEndPoint/login

Request Header:

  1. UserId=OD user
  2. Password=OD password

Kết quả nếu đăng nhập thành công:

fd1f0a7c-9545-43cb-8fbb-cff2d9610d91.authentication_md

Đăng nhập bằng tài khoản user của ứng dụng di động

Các user của ứng dụng di động được tạo bằng chức năng Mobile User các tài khoản này sẽ được map vào 1 tài khoản Operator Definition dùng chung.

Khi ứng dụng di động đăng nhập, ta dùng API:

GET ServiceEndPoint/pbs_bo_authentication_mobilelogin

Request Header:

  1. UserId=mobile user name
  2. Password=

8b288db5-720c-4dcc-88eb-dc67722ce7f3.authentication_md

Sử dụng token khi làm việc với API service


Bất kỳ request nào không phải login request đều yêu cầu token ở request header

Ví dụ liệt kê danh mục tên và địa chỉ

1c4189f2-a636-4436-8d6f-483628d7cf09.authentication_md

C#:
var client = new RestClient("http://localhost:54009/PhoebusAPI/pbs_BO_LA_NA");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer AAEAAAD/////AQAAAAAAAAAMA ....");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
jquery:

var settings = {
  "url": "http://localhost:54009/PhoebusAPI/pbs_BO_LA_NA",
  "method": "GET",
  "timeout": 0,
  "headers": {
    "Authorization": "Bearer AAEAAAD/////AQAAAAAAAAAMA ......."
  },
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

PHP
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('http://localhost:54009/PhoebusAPI/pbs_BO_LA_NA');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'Authorization' => 'Bearer AAEAAAD/////AQAAAAAAAAAMA .....'
));
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

Xem thêm



Updated on : 2021-08-13 06:05:36. by : . at T470-01.

Topic : authentication