<commit_before># Copyright 2016 Liqwyd Ltd.
#
# 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.

require 'jwt'

# Top level module for all of the core Cyclid code.
module Cyclid
  # Module for the Cyclid API
  module API
    # Module for all Auth related API endpoints
    module Auth
      # API endpoints for managing API tokens
      # @api REST
      module Token
        # rubocop:disable Metrics/LineLength
        # @!group Organizations

        # @!method post_token_username
        # @overload POST /token/:username
        # @param [String] username Username of the user to generate a token for.
        # @macro rest
        # Generate a JSON Web Token for use with the Token authentication scheme.
        # The user must authenticate using one of the other available methods
        # (HTTP Basic or HMAC) to obtain a token.
        # @return A JWT token.
        # @return [404] The user does not exist

        # @!endgroup
        # rubocop:enable Metrics/LineLength

        # Sinatra callback
        # @private
        def self.registered(app)
          include Errors::HTTPErrors

          app.post '/:username' do
            authorized_as!(params[:username], Operations::READ)

            payload = parse_request_body
            Cyclid.logger.debug payload

            user = User.find_by(username: params[:username])
            halt_with_json_response(404, INVALID_USER, 'user does not exist') \
              if user.nil?

            # Create a JSON Web Token. Use the provided payload as the intial
            # set of claims but remove some of the standard claims we don't
            # want users to be able to set.
            #
            # Requests MAY set 'exp' and 'nbf' if they wish.<commit_msg>I cant even keep Rubocop happy when writing documentation...<commit_after># Copyright 2016 Liqwyd Ltd.
#
# 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.

require 'jwt'

# Top level module for all of the core Cyclid code.
module Cyclid
  # Module for the Cyclid API
  module API
    # Module for all Auth related API endpoints
    module Auth
      # API endpoints for managing API tokens
      # @api REST
      module Token
        # @!group Tokens

        # @!method post_token_username
        # @overload POST /token/:username
        # @param [String] username Username of the user to generate a token for.
        # @macro rest
        # Generate a JSON Web Token for use with the Token authentication scheme.
        # The user must authenticate using one of the other available methods
        # (HTTP Basic or HMAC) to obtain a token.
        # @return A JWT token.
        # @return [404] The user does not exist

        # @!endgroup

        # Sinatra callback
        # @private
        def self.registered(app)
          include Errors::HTTPErrors

          app.post '/:username' do
            authorized_as!(params[:username], Operations::READ)

            payload = parse_request_body
            Cyclid.logger.debug payload

            user = User.find_by(username: params[:username])
            halt_with_json_response(404, INVALID_USER, 'user does not exist') \
              if user.nil?

            # Create a JSON Web Token. Use the provided payload as the intial
            # set of claims but remove some of the standard claims we don't
            # want users to be able to set.
            #
            # Requests MAY set 'exp' and 'nbf' if they wish.
{'fraction_non_alphanumeric': 0.06545866364665912, 'fraction_numerical': 0.0063420158550396375, 'mean_word_length': 2.4021571648690294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6522981', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1114, 'n_words': 519}