<commit_before>%%% ocs_radius_authentication.erl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This {@link //radius/radius. radius} behaviour callback
%%% 	module performs authentication procedures in the
%%% 	{@link //ocs. ocs} application.
%%%
-module(ocs_radius_authentication).
-copyright('Copyright (c) 2016 SigScale Global Inc.').

-behaviour(radius).

%% export the radius behaviour callbacks
-export([init/2, request/4, terminate/2]).

-include_lib("radius/include/radius.hrl").

-record(state,
		{port_server :: atom() | pid()}).

%%----------------------------------------------------------------------
%%  The radius callbacks
%%----------------------------------------------------------------------

-spec init(Address :: inet:ip_address(), Port :: pos_integer()) ->
	{ok, State :: #state{}} | {error, Reason :: term()}.
%% @doc This callback function is called when a
%% 	{@link //radius/radius_server. radius_server} behaviour process
%% 	initializes.
init(Address, Port) when is_tuple(Address), is_integer(Port) ->
	case global:whereis_name({ocs_radius_auth, Address, Port}) of
		PortServer when is_pid(PortServer) ->
			{ok, #state{port_server = PortServer}};
		undefined ->
			{error, radius_auth_port_server_not_found}
	end.

-spec request(Address :: inet:ip_address(), Port :: pos_integer(),
		Packet :: binary(), State :: #state{}) ->
	{ok, Response :: binary()} | {error, Reason :: ignore | term()}.
%% @doc This function is called when a request is received on the port.
%%
request(Address, Port, Packet, #state{port_server = Server} = _State)
		when is_tuple(Address) ->
	try
		{ok, SharedSecret} = ocs:find_client(Address),
		Radius = radius:codec(Packet),
		#radius{code = ?AccessRequest, attributes = AttributeData} = Radius,
		Attributes = radius_attributes:codec(AttributeData),
		case radius_attributes:find(?EAPMessage, Attributes) of
			{ok, _} ->
				MsgAuth = radius_attributes:fetch(?MessageAuthenticator,
						Attributes),
				Attr1 = radius_attributes:store(?MessageAuthenticator,
						<<0:128>>, Attributes),
				AttrBin = radius_attributes:codec(Attr1),
				Packet1 = radius:codec(Radius#radius{attributes = AttrBin}),
				MsgAuth = crypto:hmac(md5, SharedSecret, Packet1);
			{error, not_found} ->
				ok
		end,<commit_msg>fixed pattern<commit_after>%%% ocs_radius_authentication.erl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This {@link //radius/radius. radius} behaviour callback
%%% 	module performs authentication procedures in the
%%% 	{@link //ocs. ocs} application.
%%%
-module(ocs_radius_authentication).
-copyright('Copyright (c) 2016 SigScale Global Inc.').

-behaviour(radius).

%% export the radius behaviour callbacks
-export([init/2, request/4, terminate/2]).

-include_lib("radius/include/radius.hrl").

-record(state,
		{port_server :: atom() | pid()}).

%%----------------------------------------------------------------------
%%  The radius callbacks
%%----------------------------------------------------------------------

-spec init(Address :: inet:ip_address(), Port :: pos_integer()) ->
	{ok, State :: #state{}} | {error, Reason :: term()}.
%% @doc This callback function is called when a
%% 	{@link //radius/radius_server. radius_server} behaviour process
%% 	initializes.
init(Address, Port) when is_tuple(Address), is_integer(Port) ->
	case global:whereis_name({ocs_radius_auth, Address, Port}) of
		PortServer when is_pid(PortServer) ->
			{ok, #state{port_server = PortServer}};
		undefined ->
			{error, radius_auth_port_server_not_found}
	end.

-spec request(Address :: inet:ip_address(), Port :: pos_integer(),
		Packet :: binary(), State :: #state{}) ->
	{ok, Response :: binary()} | {error, Reason :: ignore | term()}.
%% @doc This function is called when a request is received on the port.
%%
request(Address, Port, Packet, #state{port_server = Server} = _State)
		when is_tuple(Address) ->
	try
		{ok, _, _, SharedSecret} = ocs:find_client(Address),
		Radius = radius:codec(Packet),
		#radius{code = ?AccessRequest, attributes = AttributeData} = Radius,
		Attributes = radius_attributes:codec(AttributeData),
		case radius_attributes:find(?EAPMessage, Attributes) of
			{ok, _} ->
				MsgAuth = radius_attributes:fetch(?MessageAuthenticator,
						Attributes),
				Attr1 = radius_attributes:store(?MessageAuthenticator,
						<<0:128>>, Attributes),
				AttrBin = radius_attributes:codec(Attr1),
				Packet1 = radius:codec(Radius#radius{attributes = AttrBin}),
				MsgAuth = crypto:hmac(md5, SharedSecret, Packet1);
			{error, not_found} ->
				ok
		end,
{'fraction_non_alphanumeric': 0.22751499000666223, 'fraction_numerical': 0.007994670219853431, 'mean_word_length': 3.9546204620462047, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '502675', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1669, 'n_words': 494}