AngularJS 를 이용하여 만든 웹 사이트에 일정시간 이후 자동으로 로그아웃 시키는 기능을 오래전부터 생각 해 왔었다.


오늘 Backend (API) 어플리케이션을 부분 리팩토링 한 후 퇴근까지 시간이 남아서 해당 기능에 대해서 조사해본 결과, setTimeout() 라는 자바스크립트에서 제공하는 타이머 함수가 있는 것을 알게되었다.


https://www.w3schools.com/jsref/met_win_settimeout.asp


이 함수를 이용해서 원하는 기능을 설정 해놓고 일정 시간 뒤에 실행할 수 있는걸 확인했다.


Window Object: clearTimeout() Method

Window Object: clearInterval() Method


위 함수들을 이용하면 타이머 취소도 가능하다.


여기서 갑자기 드는생각.


AngularJS 에 제공되는 기능도 있지 않을까?

찾아 보니 역시나! 있다.


https://docs.angularjs.org/api/ng/service/$timeout


사용방법은 JS 의 타이머 함수와 유사했다.


자, 그럼 기능을 구현해 볼까!!


기본적으로 로그아웃기능을 state('access.signout', ...) 으로 만들어져 있다.


요구사항은 이러하다

  • 로그인 이후 페이지에서 타이머가 동작해야 한다.
  • $state 가 변경될 때마다 타이머를 갱신한다. 
  • 특정시간동안 아무런 액션 없을 경우 로그아웃 시킨다.


/*

 * 특정시간이 지나면 자동으로 로그아웃 시켜주는 기능 

 */

$rootScope.initAutoLogout = function() {

// 설정된 타이머가 있을 경우 타이머 취소

if ($rootScope.autoLogoutPromise) {

$timeout.cancel($rootScope.autoLogoutPromise);    

}

// 타이머 설정

$rootScope.autoLogoutPromise = $timeout (function () {

$state.go('access.signout');

return null;

}, (15 * 60 * 1000), true); // 15 min

}

  

/*

 * $state 가 변경될때마다 호출되는 부분

 */  

$rootScope.$on('$stateChangeStart', function(event, current, previous) {

// 로그인이 필요한 페이지의 경우

if (current.requiedLogin) {

// 특정시간이 지나면 자동으로 로그아웃 기능 설정

$rootScope.initAutoLogout();

  }

});



뒷단에서 제공하는 token 의 유효시간을 20분으로 설정해놨기에 Web 에서는 그보다 조금 짧은 15분으로 세팅하고, 테스트 진행해본 결과  잘 동작한다.


위 코드에서 상황에 따라 양념을 더 쳐야 하겠지만 일단 위의 코드로 자동 로그아웃되는 기능이 추가되었다.


# 참고 사이트


1. [JavaScript] 시간 지연 함수, 일정 시간 뒤 실행시키기, setTimeout() {}

- http://webisfree.com/blog/?titlequery=javascript-%EC%8B%9C%EA%B0%84-%EC%A7%80%EC%97%B0-%ED%95%A8%EC%88%98--%EC%9D%BC%EC%A0%95-%EC%8B%9C%EA%B0%84-%EB%92%A4-%EC%8B%A4%ED%96%89%EC%8B%9C%ED%82%A4%EA%B8%B0--settimeout-


2. JS에서의 타이머와 중지 시키는 법

http://thesoul214.blogspot.kr/2013/08/js.html


3. [AngujarJS] 쉽지만 쉽지않은 $timeout 서비스 - http://programmingsummaries.tistory.com/348

Posted by CJ.Ree
,

OAuth 2.0 과련 서비스를 만들어 테스트하는 도중에 ID/PWD 를 이용한 사용자 인증시 비밀번호가 틀렸을 경우 error : invalid_grant 에러가 발생하는데 이때 HTTP Status 가 400 으로 반환된다.


400 !?


내가 알기론 400 Status 는 Bad Request 인데, client 에서 잘못된 요청이 있을 경우 발생하는 경우라 클라이언트의 요청부분을 수정해야 할 경우 발생시키는 에러로 인지하고 있는데, 비밀번호가 틀렸을 경우 400 에러를 발생시키는게 좀 의아했다.


비밀번호가 틀렸으니 401(Unauthorized) 상태를 반환해야하는것이 맞지 않을까?


그래서 문제를 제기했는데, OAuth 2.0 관련 라이브러리에서 자동으로 그렇게 만들어 주는 부분이라한다. 

관련 이슈를 조사해보니 정말 invalid_grant is 400 이었다.


라이브러리 부분을 뜯어 고치기엔 일이 커지는지라 일단 그대로 400 으로 인지 하자곤 했는데...

계속 "왜??" 라는 의문이 계속 남는다.


언제 관련 부분 조사를 좀 해봐야할듯해서 기록을 남긴다.


덧. 


# Stackoverflow 에서도 나와 유사한 의문을 가진 사람이 있었군..


http://stackoverflow.com/questions/22586825/oauth-2-0-why-does-the-authorization-server-return-400-instead-of-401-when-the


# 여기 스펙에서는 invalid_grant is 401 이다.


https://www.tutorialspoint.com/oauth2.0/error_response_codes.htm



# OAuth2 flows


http://apiwiki.poken.com/authentication/oauth2


# OAuth2 Response Codes


https://www.docusign.com/p/RESTAPIGuide/Content/OAuth2/OAuth2%20Response%20Codes.htm


OAuth2 Response Codes

The OAuth2 –related endpoints return 200-OK on success. On failure, they typically return 400-Bad Request, or 401-Unauthorized.

In order to be consistent with OAuth2 documentation, the OAuth2-related endpoints support a different error response format than other DocuSign REST API calls. The body of an error response to the “oauth2/token” and “oauth2/revoke” endpoints have the format:

{

"error":"<error code>",

"error_description":"<optional description>"

}

 

Oauth2/token Endpoint error codes:

Error code values are all lower-case to match OAuth2 documentation.

Error CodeError Description
invalid_requestThe request was malformed, or contains unsupported parameters
invalid_clientThe client authentication failed.
invalid_grantThe provided authorization is invalid.
unauthorized_clientThe client application is not allowed to use this grant_type.
unsupported_grant_typeA grant_type other than “password” was used in the request.
invalid_scopeThe scope was not set to “api”.

 

OAuth2/revoke Endpoint error codes

Error code values are all lower-case to match OAuth2 documentation.

Error CodeError Description
unsupported_token_typeThe client tried to revoke an access token on a server not supporting this feature. This error is not supported in DocuSign.
invalid_tokenThe presented token is invalid.

 



Posted by CJ.Ree
,

# Before

SHOW FULL PROCESSLIST;  


# After

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER = 'chang-jae.lee';

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST ORDER BY TIME DESC;



참고 : http://stackoverflow.com/questions/929612/how-to-customize-show-processlist-in-mysql

Posted by CJ.Ree
,