<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Canvas::Security do
  describe "JWT tokens" do
    describe "encoding" do
      after(:each) do
        Timecop.return
      end

      describe ".create_jwt" do
        it "should generate a token with an expiration" do
          Timecop.freeze(Time.utc(2013,3,13,9,12))
          expires = 1.hour.from_now
          token = Canvas::Security.create_jwt({ a: 1 }, expires)
          expected_token = "<KEY>\
                           "<KEY>\
                           "VwDKl46gfjFLPAIDwlkVPze1UwC6H_ApdyWYoUXFT8M"
          expect(token).to eq(expected_token)
        end

        it "should generate a token without expiration" do
          token = Canvas::Security.create_jwt({ a: 1 })
          expected_token = "<KEY>\
                           "<KEY>\
                           "<KEY>"
          expect(token).to eq(expected_token)
        end

        it "should encode with configured encryption key" do
          jwt = stub
          jwt.expects(:sign).with(Canvas::Security.encryption_key, :HS256).returns("sometoken")
          JSON::JWT.stubs(new: jwt)
          Canvas::Security.create_jwt({ a: 1 })
        end

        it "should encode with the supplied key" do
          jwt = stub
          jwt.expects(:sign).with("mykey", :HS256).returns("sometoken")
          JSON::JWT.stubs(new: jwt)
          Canvas::Security.create_jwt({ a: 1 }, nil, "mykey")
        end
      end

      describe ".create_encrypted_jwt" do
        let(:signing_secret){ "asdfasdfasdfasdfasdfasdfasdfasdf" }
        let(:encryption_secret){ "<KEY> }

        it "builds up an encrypted token" do
          payload = {arbitrary: "data"}
          jwt = Canvas::Security.create_encrypted_jwt(payload, signing_secret, encryption_secret)
          expect(jwt.length).to eq(225)
        end
      end
    end

    describe ".base64_encode" do
      it "trims off newlines" do
        input = "SuperSuperSuperSuperSuperSuperSuperSuper"\
                 "SuperSuperSuperSuperSuperSuperSuperSuperLongString"
        output = "U3VwZXJTdXBlclN1cGVyU3VwZXJTdXBlclN1cGVy"\
                 "U3VwZXJTdXBlclN1cGVyU3VwZXJTdXBlclN1cGVy"\
                 "U3VwZXJTdXBlclN1cGVyU3VwZXJMb25nU3RyaW5n"
        expect(Canvas::Security.base64_encode(input)).to eq(output)
      end
    end

    describe "decoding" do
      before do
        Timecop.freeze(Time.utc(2013,3,13,9,12))
        @key = "mykey"
        @token_no_expiration = JSON::JWT.new({ a: 1 }).sign(@key, :HS256).to_s
        @token_expired = JSON::JWT.new({ a: 1, 'exp' => 1.hour.ago.to_i }).sign(@key, :HS256).to_s
        @token_not_expired = JSON::JWT.new({ a: 1, 'exp' => 1.hour.from_now.to_i }).sign(@key, :HS256).to_s
      end

      after do
        Timecop.return
      end

      it "should decode token" do
        body = Canvas::Security.decode_jwt(@token_no_expiration, [ @key ])
        expect(body).to eq({ "a" => 1 })
      end
<commit_msg>spec: prefer block syntax for timecop<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Canvas::Security do
  describe "JWT tokens" do
    describe "encoding" do
      describe ".create_jwt" do
        it "should generate a token with an expiration" do
          Timecop.freeze(Time.utc(2013,3,13,9,12)) do
            expires = 1.hour.from_now
            token = Canvas::Security.create_jwt({ a: 1 }, expires)

            expected_token = "<KEY>\
                             "<KEY>\
                             "VwDKl46gfjFLPAIDwlkVPze1UwC6H_ApdyWYoUXFT8M"
            expect(token).to eq(expected_token)
          end
        end

        it "should generate a token without expiration" do
          token = Canvas::Security.create_jwt({ a: 1 })
          expected_token = "<KEY>\
                           "<KEY>\
                           "<KEY>"
          expect(token).to eq(expected_token)
        end

        it "should encode with configured encryption key" do
          jwt = stub
          jwt.expects(:sign).with(Canvas::Security.encryption_key, :HS256).returns("sometoken")
          JSON::JWT.stubs(new: jwt)
          Canvas::Security.create_jwt({ a: 1 })
        end

        it "should encode with the supplied key" do
          jwt = stub
          jwt.expects(:sign).with("mykey", :HS256).returns("sometoken")
          JSON::JWT.stubs(new: jwt)
          Canvas::Security.create_jwt({ a: 1 }, nil, "mykey")
        end
      end

      describe ".create_encrypted_jwt" do
        let(:signing_secret){ "asdfasdfasdfasdfasdfasdfasdfasdf" }
        let(:encryption_secret){ "jkl;jkl;jkl;jkl;jkl;jkl;jkl;jkl;" }

        it "builds up an encrypted token" do
          payload = {arbitrary: "data"}
          jwt = Canvas::Security.create_encrypted_jwt(payload, signing_secret, encryption_secret)
          expect(jwt.length).to eq(225)
        end
      end
    end

    describe ".base64_encode" do
      it "trims off newlines" do
        input = "SuperSuperSuperSuperSuperSuperSuperSuper"\
                 "SuperSuperSuperSuperSuperSuperSuperSuperLongString"
        output = "U3VwZXJTdXBlclN1cGVyU3VwZXJTdXBlclN1cGVy"\
                 "U3VwZXJTdXBlclN1cGVyU3VwZXJTdXBlclN1cGVy"\
                 "U3VwZXJTdXBlclN1cGVyU3VwZXJMb25nU3RyaW5n"
        expect(Canvas::Security.base64_encode(input)).to eq(output)
      end
    end

    describe "decoding" do
      around(:example) do |example|
        Timecop.freeze(Time.utc(2013,3,13,9,12)) do
          @key = "mykey"
          @token_no_expiration = JSON::JWT.new({ a: 1 }).sign(@key, :HS256).to_s
          @token_expired = JSON::JWT.new({ a: 1, 'exp' => 1.hour.ago.to_i }).sign(@key, :HS256).to_s
          @token_not_expired = JSON::JWT.new({ a: 1, 'exp' => 1.hour.from_now.to_i }).sign(@key, :HS256).to_s
          example.run
        end
      end

      it "should decode token" do
        body = Canvas::Security.decode_jwt(@token_no_expiration, [ @key ])
        expect(body).to eq({ "a" => 1 })
      end

{'fraction_non_alphanumeric': 0.10775277853076715, 'fraction_numerical': 0.02114394144754676, 'mean_word_length': 2.397329650092081, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 22, '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': '5194883', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2359, 'n_words': 601}